मैं अपने ऐप के भीतर प्रोफ़ाइल चित्र परिवर्तनों को कार्यान्वित करने का उदाहरण ढूंढने के लिए उच्च और निम्न खोज रहा हूं। मैं लोगों को Parse.com (आज सभी सोशल मीडिया ऐप्स के समान) का उपयोग करके अपनी प्रोफ़ाइल छवि को जोड़ने/बदलने की अनुमति देना चाहता हूं।प्रोफ़ाइल बनाने के लिए अनुमति देने वाले ऐप को कैसे अपलोड करें/बदलें?
उदाहरण के लिए: ट्विटर, फेसबुक, इंस्टाग्राम और इसी तरह, वे सभी आपको प्रोफ़ाइल छवि लेने/अपलोड करने की अनुमति देते हैं और वह छवि सहेजी जाती है और बाद में देखी जा सकती है।
मैं किसी भी सामग्री कैसे ऐसा करने के लिए शामिल किया गया है कि खोजने कोई भाग्य पड़ा है और कोई और नहीं मैं क्या यहाँ प्राप्त करने के लिए कोशिश कर रहा हूँ समझने किया जा रहा है: मेरे एप्लिकेशन में अब तक
An Image Downloaded From Parse Stay On Screen Even After You Exit And Reopen App?
उपयोगकर्ता कैमरे के इरादे से एक तस्वीर ले सकता है या गैलरी से एक छवि अपलोड कर सकता है और वह छवि पूरी तरह से एक छवि दृश्य में प्रदर्शित होती है।
समस्या है: जब मैं बाहर निकलें और एप्लिकेशन छवि देखें नहीं रह प्रदर्शित किया जाता है (यह चला गया है) के अंदर छवि को फिर से खोलने।
मैं कैसे इस समस्या को हल कर सकते हैं?
MainActivity:
public class MainActivity extends AppCompatActivity {
public static final int TAKE_PIC_REQUEST_CODE = 0;
public static final int CHOOSE_PIC_REQUEST_CODE = 1;
public static final int MEDIA_TYPE_IMAGE = 2;
private Uri mMediaUri;
private TextView mChangeProfilePic;
protected ImageView mPreviewImageView;
private Button mSaveChangesBtn;
public ImageView mProfilePic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//Initialize variables
mChangeProfilePic = (TextView) findViewById(R.id.changeProfileImageTxt);
mPreviewImageView = (ImageView) findViewById(R.id.profileImage);
mSaveChangesBtn = (Button) findViewById(R.id.saveProfileChangesBtn);
mSaveChangesBtn.setEnabled(false);
final Button mNextBtn = (Button) findViewById(R.id.NextBtn);
mNextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentNext = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intentNext);
}
});
//Change profile image
//set onlClick to TextView
mChangeProfilePic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Change Pic Pressed", Toast.LENGTH_SHORT).show();
//show dialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Upload or Take a photo");
builder.setPositiveButton("Upload", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//upload image
Intent choosePictureIntent = new Intent(Intent.ACTION_GET_CONTENT);
choosePictureIntent.setType("image/*");
startActivityForResult(choosePictureIntent, CHOOSE_PIC_REQUEST_CODE);
mSaveChangesBtn.setEnabled(true);
}
});
builder.setNegativeButton("Take Photo", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//take photo
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
if (mMediaUri == null) {
//display error
Toast.makeText(getApplicationContext(), "Sorry there was an error! Try again.", Toast.LENGTH_LONG).show();
mSaveChangesBtn.setEnabled(false);
} else {
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(takePicture, TAKE_PIC_REQUEST_CODE);
mSaveChangesBtn.setEnabled(true);
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});//End change profile image onClick Listener
//Save profile changes button
//Also uploads content to parse and pulls it back same time
mSaveChangesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//create parse object for image to upload
final ParseObject imageUpload = new ParseObject("ImageUploads");
try {
//convert image to bytes for upload.
byte[] fileBytes = FileHelper.getByteArrayFromFile(MainActivity.this, mMediaUri);
if (fileBytes == null) {
//there was an error
Toast.makeText(getApplicationContext(), "There was an error. Try again!", Toast.LENGTH_LONG).show();
mSaveChangesBtn.setEnabled(false);
} else {
fileBytes = FileHelper.reduceImageForUpload(fileBytes);
String fileName = FileHelper.getFileName(MainActivity.this, mMediaUri, "image");
final ParseFile file = new ParseFile(fileName, fileBytes);
imageUpload.saveEventually(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
imageUpload.put("imageContent", file);
imageUpload.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
Toast.makeText(getApplicationContext(), "Success Uploading iMage!", Toast.LENGTH_LONG).show();
//Retrieve the recently saved image from Parse
queryParseProfileImages(imageUpload);
mSaveChangesBtn.setEnabled(false);
}
}
);
} else {
//there was an error
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
mSaveChangesBtn.setEnabled(false);
}
}
});
}
} catch (Exception e1) {
Toast.makeText(getApplicationContext(), e1.getMessage(), Toast.LENGTH_LONG).show();
}
}//End onClick(View v)
});//End onClick Listener
//This method queries for the most recent picture taken
ParseQuery<ParseObject> imagesQuery = new ParseQuery<>("ImageUploads");
imagesQuery.orderByDescending("createdAt");
imagesQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> images, ParseException e) {
if(e == null){
//for (int i = 0; i < images.size(); i++) {
final String imgUrl = images.get(0).getParseFile("imageContent").getUrl();
mProfilePic = (ImageView) findViewById(R.id.profileImage);
Picasso.with(MainActivity.this).load(imgUrl).into(mProfilePic);
//}
//images.pinInBackground();
//profileImageId = profImgObj.getObjectId();
//Log.d(TAG, "The object id is: " + profileImageId);
}else{
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}//End onCreate
//Method containing ParseQuery to download/pull back the image that was uploaded to Parse
//Inside the Image View
private void queryParseProfileImages(final ParseObject imageUploadPassed) {
ParseFile userImageRetrievedObj = (ParseFile) imageUploadPassed.get("imageContent");
userImageRetrievedObj.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
final String imgUrl = imageUploadPassed.getParseFile("imageContent").getUrl();
mProfilePic = (ImageView) findViewById(R.id.profileImage);
Picasso.with(MainActivity.this).load(imgUrl).into(mProfilePic);
imageUploadPassed.pinInBackground();
} else {
// something went wrong
}
}
});
}
//inner helper method
private Uri getOutputMediaFileUri(int mediaTypeImage) {
if (isExternalStorageAvailable()) {
//get the URI
//get external storage dir
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "UPLOADIMAGES");
//create subdirectore if it does not exist
if (!mediaStorageDir.exists()) {
//create dir
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
//create a file name
//create file
File mediaFile = null;
Date now = new Date();
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(now);
String path = mediaStorageDir.getPath() + File.separator;
if (mediaTypeImage == MEDIA_TYPE_IMAGE) {
mediaFile = new File(path + "IMG_" + timestamp + ".jpg");
}
//return file uri
Log.d("UPLOADIMAGE", "FILE: " + Uri.fromFile(mediaFile));
return Uri.fromFile(mediaFile);
} else {
return null;
}
}
//check if external storage is mounted. helper method
private boolean isExternalStorageAvailable() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CHOOSE_PIC_REQUEST_CODE) {
if (data == null) {
Toast.makeText(getApplicationContext(), "Image cannot be null!", Toast.LENGTH_LONG).show();
} else {
mMediaUri = data.getData();
//set previews
mPreviewImageView.setImageURI(mMediaUri);
//Bundle extras = data.getExtras();
//Log.e("URI", mMediaUri.toString());
//Bitmap bmp = (Bitmap) extras.get("data");
}
} else {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(mMediaUri);
sendBroadcast(mediaScanIntent);
//set previews
mPreviewImageView.setImageURI(mMediaUri);
}
} else if (resultCode != RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Cancelled!", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
आपकी प्रतिक्रिया के लिए बहुत बहुत धन्यवाद और आपकी तारीफ के लिए मैंने सोचा कि मुझे इस प्रश्न के साथ फंसे रहने वाले किसी भी मदद के बिना फंसे रहेंगे ताकि आपकी मदद की सराहना की जा सके। जब मैं घर जाता हूं तो मैं आपके दृष्टिकोण की कोशिश करूंगा और यदि मैं किसी और समस्या में भाग लेता हूं तो मैं आपको बता दूंगा। – Equivocal
मैंने कोड में बदलाव किए हैं और यह अभी भी काम नहीं करता है, मैंने शायद आपने जो कहा है वह नहीं किया हो लेकिन मुझे यकीन नहीं है। मैंने जो किया वह ऑनक्रेट के बाहर एक विधि बनाने के लिए था और उस विधि के अंदर क्वेरी की सामग्री को उस विधि को कॉल करने के बजाय ऑनक्रिक के अंदर क्लिक करें। – Equivocal
हां, सही है लेकिन आप इसे 'ऑनक्रेट' में भी कॉल करना भूल गए हैं, है ना? इसे पहली बार लोड करने के लिए आपको इसे 'ऑनक्रेट' में भी कॉल करने की आवश्यकता है। –