簡體   English   中英

如何在共享的首選項中保存裁剪的圖像uri

[英]How to save cropped image uri in shared Preference

我選擇了一張圖像並裁剪了它。 但我想將裁剪后的圖像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,以便以后可以檢索圖像。

要保存它:

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

根據我之前提供的鏈接寫了這個...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM