ImageView在安卓设备重启时无法保留图像。

huangapple go评论63阅读模式
英文:

ImageView not retaining Image when android device restart

问题

我从图库中选择了图片并在ImageView中显示但是在安卓设备重新启动时没有保留或保存图像我需要重新选择一张图片我的计划是即使在设备重新启动时图像仍然保留在ImageView中还是我需要创建一些数据来保存图像并显示到ImageView中

public class FirstFragment extends Fragment implements View.OnClickListener{
    ImageView imageButton1;

    private Uri mImageUri;

    @Override
    public void onResume() {
        super.onResume();
    }

    private File mSnapFile;

    private static final String ARG_URI_IMAGE_1 = "image1Uri";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_first, container, false);
        imageButton1 = (ImageView) v.findViewById(R.id.firstimagebtn);

        imageButton1.setOnClickListener(this::onClick);

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String mImageUri = preferences.getString("image", null);

        if (mImageUri != null) {
            imageButton1.setImageURI(Uri.parse(mImageUri));
        }
        return v;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.firstimagebtn:
                Intent intent;
                if (Build.VERSION.SDK_INT < 19) {
                    intent = new Intent(Intent.ACTION_GET_CONTENT);
                } else {
                    intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                }
                intent.setType("image/*");
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), 0);
                break;

        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode) {
            case 0:
                if(resultCode == Activity.RESULT_OK){
                    if (data != null) {
                        // This is the key line item, URI specifies the name of the data
                        mImageUri = data.getData();
                        // Saves image URI as string to Default Shared Preferences
                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString("image", String.valueOf(mImageUri));
                        editor.commit();
                        // Sets the ImageView with the Image URI
                        imageButton1.setImageURI(mImageUri);
                        imageButton1.invalidate();
                    }
                }
                break;
        }
    }
}
英文:

I have images selected from the gallery displayed in ImageView, But not retaining or saved Image when the android device restart I need to re-pick an image again. My plan is for the image to still stay on the image view even the device is rebooted , or do I need to create some data to save the image and display to ImageView

public class FirstFragment extends Fragment implements View.OnClickListener{
ImageView imageButton1;
private Uri mImageUri;
@Override
public void onResume() {
super.onResume();
}
private File mSnapFile;
private static final String ARG_URI_IMAGE_1 = &quot;image1Uri&quot;;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_first, container, false);
imageButton1 = (ImageView) v.findViewById(R.id.firstimagebtn);
imageButton1.setOnClickListener(this::onClick);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String mImageUri = preferences.getString(&quot;image&quot;, null);
if (mImageUri != null) {
imageButton1.setImageURI(Uri.parse(mImageUri));
} 
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.firstimagebtn:
Intent intent;
if (Build.VERSION.SDK_INT &lt; 19) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
} else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
}
intent.setType(&quot;image/*&quot;);
startActivityForResult(Intent.createChooser(intent, &quot;Select Picture&quot;), 0);
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 0:
if(resultCode == Activity.RESULT_OK){
if (data != null) {
// This is the key line item, URI specifies the name of the data
mImageUri = data.getData();
// Saves image URI as string to Default Shared Preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = preferences.edit();
editor.putString(&quot;image&quot;, String.valueOf(mImageUri));
editor.commit();
// Sets the ImageView with the Image URI
imageButton1.setImageURI(mImageUri);
imageButton1.invalidate();
}
}
break;
case 1:
if(resultCode == Activity.RESULT_OK){
// This is the key line item, URI specifies the name of the data
mImageUri2 = data.getData();
// Saves image URI as string to Default Shared Preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = preferences.edit();
editor.putString(&quot;image2&quot;, String.valueOf(mImageUri2));
editor.commit();
// Sets the ImageView with the Image URI
imageButton2.setImageURI(mImageUri2);
imageButton2.invalidate();
}
break;
}
}

答案1

得分: 0

你可以使用像Glide或Picasso这样的图像加载库。它们提供数据缓存,您的图像将被保存。

查看文档

默认情况下,Glide在开始新的图像请求之前会检查多层缓存:

1.活动资源 - 此图像当前是否在另一个视图中显示?

2.内存缓存 - 此图像是否最近已加载并仍在内存中?

3.资源 - 此图像是否已解码、转换并写入磁盘缓存?

4.数据 - 获取此图像的数据是否先前写入磁盘缓存?

前两个步骤检查资源是否在内存中,如果是,则立即返回图像。后两个步骤检查图像是否在磁盘上,并快速异步返回。

如果这四个步骤都无法找到图像,那么Glide将返回原始来源以检索数据(原始文件、Uri、Url等)。

希望对您有所帮助。如果在使用中遇到问题,请告诉我,但我认为文档本身已经相当详细了。

英文:

You can use image loading libraries like Glide or Picasso. They provide data caching and your image will be persisted.

See docs

By default, Glide checks multiple layers of caches before starting a new request for an image:

1.Active resources - Is this image displayed in another View right now?

2.Memory cache - Was this image recently loaded and still in memory?

3.Resource - Has this image been decoded, transformed, and written to the disk cache before?

4.Data - Was the data this image was obtained from written to the disk cache before?

The first two steps check to see if the resource is in memory and if so, return the image immediately. The second two steps check to see if the image is on disk and return quickly, but asynchronously.

If all four steps fail to find the image, then Glide will go back to the original source to retrieve the data (the original File, Uri, Url etc).

Hope this helps. Let me know if you face an issue with this but I think the docs are pretty much self explanatory.

huangapple
  • 本文由 发表于 2020年10月21日 16:10:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64459330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定