gpt4 book ai didi

android - 如何使用共享首选项将多个图像保存到 ImageView

转载 作者:搜寻专家 更新时间:2023-11-01 08:58:08 25 4
gpt4 key购买 nike

我有几个 ImageView 的 Activity 。用户可以长按 ImageView,他们可以选择从相册中获取任何图像。我试图保存这些图像的图像路径,以便当用户关闭并再次打开应用程序时,图像仍将位于 imageView 中。我不明白为什么这不起作用。

这是我的 Activity A,其中有 imageView

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
im1.setImageURI(selectedImageUri);}}}

public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);};

@Override
protected void onPause() {
SharedPreferences sp = getSharedPreferences("AppSharedPref", 1); // Open SharedPreferences with name AppSharedPref
Editor editor = sp.edit();
editor.putString("ImagePath", selectedImagePath); // Store selectedImagePath with key "ImagePath". This key will be then used to retrieve data.
editor.commit();
super.onPause();
}

protected void onResume1() {
SharedPreferences sp = getSharedPreferences("AppSharedPref", 1);
selectedImagePath = sp.getString("ImagePath", "");
super.onResume();
}

Activity B 获取 cam gallery pic 并发送回 Activity A

 Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent();
setResult(RESULT_OK, intent);
Bundle bundle=new Bundle();
bundle.putInt("image",R.id.showImg);
intent.putExtras(bundle);
finish(); }
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}}}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

最佳答案

制作:

@Override
protected void onResume() {
SharedPreferences sp = getSharedPreferences("AppSharedPref", 1);
selectedImagePath = sp.getString("ImagePath", "");
Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath);
im1.setImageBitmap(myBitmap);
super.onResume();
}

代替:

protected void onResume1() {
SharedPreferences sp = getSharedPreferences("AppSharedPref", 1);
selectedImagePath = sp.getString("ImagePath", "");
super.onResume();
}

因为您的 Activity 扩展了 Activity 类,该类声明并实现了此方法。此方法在 Activity 返回前台后调用。您想要的是覆盖此方法,以便它执行您希望它执行的操作。

关于android - 如何使用共享首选项将多个图像保存到 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17358820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com