繁体   English   中英

应用关闭后,从图库中选择的图像将从Imageview中消失

[英]Image chosen from gallery disappears from Imageview after application closes

希望您能帮我解决我的简单问题。 我正在尝试从图库中选择要显示在Imageview上的图像,它可以工作,但事实是,关闭我的应用程序后它消失了。 请检查我在下面找到的代码。

iv=(ImageView) rootView.findViewById(R.id.profpic);
            iv.setOnClickListener(this);



    private void showFileChooser() {

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
        intent.setType("image/*"); 
        intent.addCategory(Intent.CATEGORY_OPENABLE);

        try {
            startActivityForResult(
                    Intent.createChooser(intent, "Select a File to Upload"),
                    FILE_SELECT_CODE);
        } catch (android.content.ActivityNotFoundException ex) {
            // Potentially direct the user to the Market with a Dialog
            Toast.makeText(getActivity(), "Please install a File Manager.", 
                 Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case FILE_SELECT_CODE:
            if (resultCode == Activity.RESULT_OK) {
                // Get the Uri of the selected file 
                Uri uri = data.getData();
                iv.setImageURI(uri);
                Log.d(TAG, "File Uri: " + uri.toString());
               }
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

和我的xml:

  <ImageView
        android:id="@+id/profpic"
        android:layout_width="113dp"
        android:layout_height="113dp"
        android:paddingRight="1dp"
        android:src="@drawable/propic" />

我是android开发的新手,将不胜感激。 提前致谢!

上传图片后,它不会永远保留下去。 您需要将所选图像的路径保留在Sqlite数据库SharedPreference中 然后在启动活动时,您需要使用图像路径加载到ImageView

暂无
暂无

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

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