【问题标题】:ImageView not retaining Image when android device restartandroid设备重启时ImageView不保留图像
【发布时间】:2021-02-04 02:49:51
【问题描述】:

我从 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;
        }
    }

【问题讨论】:

    标签: java android xml android-layout imageview


    【解决方案1】:

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

    See docs

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

    1.Active resources - 此图像现在是否显示在另一个 View 中?

    2. 内存缓存 - 这张图片是最近加载的并且还在内存中吗?

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

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

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

    如果四个步骤都没有找到图片,那么 Glide 将返回原始源来检索数据(原始 File、Uri、Url 等)。

    希望这会有所帮助。如果您对此有任何疑问,请告诉我,但我认为这些文档几乎是不言自明的。

    【讨论】:

    • 是否可以将图像路径保存到 SQLlite 然后检索?
    • 是的,可以这样做,但这是一种更乏味的方法,而且还容易受到错误的影响,因为您必须从头开始实现它,as answered in this question。 Gilde 和 Picasso 等图书馆已经实现了所有这些(以及更多),因此它们更安全、更易于使用。
    • 很酷,我已经制作了一些 sratch 应用程序,然后我在 android 10 和 11 时看到了一些错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多