Sorry for my English. My app take a photo and after i use picasso to load in a image view and save the image Uri in sharedpreferences. the image load fine when take or select the picture but when close the activity and reopen the image not show. What happen?
private SharedPreferencesManager mSharedPreferencesManager;
private ImageView imageProfile;
private Uri mMediaUri;
private DialogInterface.OnClickListener mDialogClickListenerToNewMessage = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
//Take Picture
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMediaUri = getOutPutMediaUri(AppConstants.MEDIA_TYPE_IMAGE);
if (mMediaUri == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(MyProfileActivity.this);
builder.setMessage(R.string.problem_accessing_the_memory_of_your_device)
.setTitle(R.string.sorry_)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialogMemory = builder.create();
dialogMemory.show();
} else {
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(takePhotoIntent, AppConstants.TAKE_PHOTO_REQUEST);
}
break;
case 1:
//Choose Picture
Intent choosePhotoIntent = new Intent(Intent.ACTION_GET_CONTENT);
choosePhotoIntent.setType("image/*");
startActivityForResult(choosePhotoIntent, AppConstants.PICK_PHOTO_REQUEST);
break;
default:
break;
}
}
};
private Uri getOutPutMediaUri(int mediaType) {
if (isExternalStoregeAvailable()) {
//get Uri
//1.- Get the external storage directory
String appName = getString(R.string.app_name);
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),appName);
//2.- Create our subdirectory
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdir()) {
AlertDialog.Builder builder = new AlertDialog.Builder(MyProfileActivity.this);
builder.setMessage(R.string.failed_to_created_directory_)
.setTitle(R.string.sorry_)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialogMemory = builder.create();
dialogMemory.show();
return null;
}
}
//3.- Create a file name
File mediaFile;
Date currentDate = new Date();
String timesTamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(currentDate);
//4.- Crate the file
String path = mediaStorageDir.getPath() + File.separator;
if (mediaType == AppConstants.MEDIA_TYPE_IMAGE) {
mediaFile = new File(path + "IMG_" + timesTamp + ".jpg");
} else if (mediaType == AppConstants.MEDIA_TYPE_VIDEO) {
mediaFile = new File(path + "VID_" + timesTamp + ".mp4");
} else {
return null;
}
//5.- Return the file's URI
return Uri.fromFile(mediaFile);
} else {
return null;
}
}
private boolean isExternalStoregeAvailable(){
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) {
if (resultCode == RESULT_OK) {
//add file to the Gallery
if (requestCode == AppConstants.PICK_PHOTO_REQUEST) {
if (data == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.failed_to_get_a_file_from_the_gallery)
.setTitle(R.string.sorry_)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
mMediaUri = data.getData();
mSharedPreferencesManager.savePhotoUri(mMediaUri.toString());
}
} else {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(mMediaUri);
sendBroadcast(mediaScanIntent);
mSharedPreferencesManager.savePhotoUri(mMediaUri.toString());
}
Picasso.with(MyProfileActivity.this).load(mMediaUri).into(imageProfile);
} else if (resultCode != RESULT_CANCELED) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.error_adding_file_to_gallery)
.setTitle(R.string.sorry_)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
My code in onResume method
mSharedPreferencesManager = new SharedPreferencesManager(this);
final String uri = mSharedPreferencesManager.getPhotoUri();
if (!uri.equals("null")) {
Uri imageUri = Uri.parse(uri);
Picasso.with(MyProfileActivity.this).load(imageUri).into(imageProfile);
}
//Code to get photo uri in sharedpreferences manager
public String getPhotoUri(){
String photoUri = sharedPreferences.getString(PHOTO_URI, "null");
return photoUri;
}
if (uri != "null") {
Uri imageUri = Uri.parse(uri);
Picasso.with(MyProfileActivity.this).load(imageUri).into(imageProfile, new Callback() {
@Override
public void onSuccess() {
// TODO Auto-generated method stub
Toast.makeText(MyProfileActivity.this, "ok", Toast.LENGTH_SHORT).show();
}
@Override
public void onError() {
// TODO Auto-generated method stub
Toast.makeText(MyProfileActivity.this, "error", Toast.LENGTH_SHORT).show();
}
});
}