【问题标题】:Image chosen from gallery disappears from Imageview after application closes应用程序关闭后,从图库中选择的图像从 Imageview 中消失
【发布时间】:2014-08-29 03:31:45
【问题描述】:

我希望你能帮助我解决我的简单问题。我正在尝试从我的画廊中选择要在我的 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 开发的新手,我们将不胜感激。提前致谢!

【问题讨论】:

  • Activity被销毁,图片也被销毁

标签: android imageview filechooser


【解决方案1】:

图片一旦上传,就不会永远保留。您需要将所选图像的路径保存在Sqlite databaseSharedPreference 中。然后在启动您的活动时,您需要使用图片路径加载到ImageView

【讨论】:

  • 感谢您的信息。我将尝试对此进行更多研究。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多