繁体   English   中英

android设备重启时ImageView不保留图像

[英]ImageView not retaining Image when android device restart

我从 ImageView 中显示的图库中选择了图像,但在 android 设备重新启动时没有保留或保存图像,我需要再次重新选择图像。 我的计划是即使设备重新启动,图像仍然保留在图像视图中,或者我是否需要创建一些数据来保存图像并显示到 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;
            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("image2", String.valueOf(mImageUri2));
                    editor.commit();
                    // Sets the ImageView with the Image URI
                    imageButton2.setImageURI(mImageUri2);
                    imageButton2.invalidate();
                }
                break;
        }
    }

您可以使用 Glide 或 Picasso 等图像加载库。 它们提供数据缓存,您的图像将被持久化。

查看文档

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

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

2.Memory cache - 这个图片是最近加载的还是在内存中?

3.Resource - 这个图像之前是否被解码、转换并写入磁盘缓存?

4.Data - 这个图片的数据是不是之前写入磁盘缓存的?

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

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

希望这可以帮助。 如果您遇到此问题,请告诉我,但我认为这些文档几乎是不言自明的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM