Developer Kid
Developer Kid

Reputation: 71

How to save cropped image uri in shared Preference

I have selected an image and cropped it. But I want to save the cropped image uri in shared preference so that it can be showed later. I know how to save in shared preference, but the problem key is "how I can get the image URL of the cropped image"

........................
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, getString(R.string.image_action)),
                Code);
........................

And in onActivtyResult() I retrieve it:

if (resultCode == Activity.RESULT_OK) {
    if (requestCode == SELECT_IMAGE) {
              Bundle extras = data.getExtras();
          Bitmap photo = extras.getParcelable("data");
              imageView.setImageBitmap(bm); 
              // I want to save the cropped bitmap image's url into preference here
        } 
}

I can save the bitmap in preference in Base64 format, but it is not recommended to save such huge data in preference. How can I save only the url of the new cropped image so that I can retrieve the image later.

Upvotes: 0

Views: 1720

Answers (2)

Larry McKenzie
Larry McKenzie

Reputation: 3283

Wrote this based on the link I provided earlier...

...
if (resultCode == Activity.RESULT_OK) {
    if (requestCode == SELECT_IMAGE) {
          Bundle extras = data.getExtras();
          Uri filePathFromActivity = (Uri) extras.get(Intent.EXTRA_STREAM);
          filePathFromActivity = Uri.parse(getRealPathFromUri( (Activity) CurrentActivity.this, filePathFromActivity));
          imagePath = filePathFromActivity.getPath();
          SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
          SharedPreferences.Editor editor = settings.edit();
          editor.putString("imagePath", imagePath);

          // Commit the edits!
          editor.commit();
    } 
}
...

public String getRealPathFromUri(Activity activity, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

Upvotes: 1

Larry McKenzie
Larry McKenzie

Reputation: 3283

To save it:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("imageUrl", imageUrl);

// Commit the edits!
editor.commit();

To retrieve it:

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
 String imageUrl = settings.getString("imageUrl", null);

From the documentation: http://developer.android.com/guide/topics/data/data-storage.html#pref

Upvotes: 0

Related Questions