我选择了一张图片并将其剪裁。但我想以共享的偏好保存裁剪后的图像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
格式保存位图,但不建议优先保存这些大数据。如何只保存新裁剪图像的网址,以便稍后检索图像。
答案 0 :(得分: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
答案 1 :(得分:0)
根据我之前提供的链接写了这个......
...
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);
}