如何保存在共享preference裁剪图像URI [英] How to save cropped image uri in shared Preference

查看:122
本文介绍了如何保存在共享preference裁剪图像URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我选择的是图像和裁剪它。但我想保存剪裁的图片URI共享preference,以便它可以在以后显示。我知道如何在共享preference保存,但问题关键是我怎样才能得到裁切图像的图像URL

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);
........................

而在 onActivtyResult()我找回它:

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
        } 
}

我可以节省preference的位图的Base64 格式,但不建议保存在preference这样庞大的数据。我怎么只能保存新的裁剪图片的URL,这样我以后可以检索图像。

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.

推荐答案

写这基于我所提供的链接在早期...

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);
}

这篇关于如何保存在共享preference裁剪图像URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆