0

我选择了一张图片并裁剪了它。但我想将裁剪后的图像 uri 保存在共享首选项中,以便以后显示。我知道如何保存共享首选项,但问题的关键是“如何获取裁剪图像的图像 URL”

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

我可以优先保存位图的Base64格式,但不建议优先保存这么大的数据。如何仅保存新裁剪图像的 url,以便稍后检索图像。

4

2 回答 2

1

根据我之前提供的链接写了这个......

...
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);
}
于 2013-05-24T20:23:01.850 回答
0

要保存它:

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

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

要检索它:

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

从文档:http: //developer.android.com/guide/topics/data/data-storage.html#pref

于 2013-05-24T19:57:51.467 回答