【问题标题】:Display image stored in internal storage in an ImageView ? What am I doing wrong?在 ImageView 中显示存储在内部存储器中的图像?我究竟做错了什么?
【发布时间】:2012-08-12 12:06:56
【问题描述】:

我首先以这种方式将图像存储在内部存储中:

FileOutputStream fos = context.openFileOutput("myimage.png", Context.MODE_PRIVATE);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.close();

然后我以这种方式将其显示在 ImageView 中:

File filepath = context.getFileStreamPath("myimage.png");
Uri uri = Uri.parse(filepath.toString());
myImageView.setImageUri(uri);

但图像从未出现在ImageView 中。由于某些限制,我不能在这里使用setImageBitmap()。谁能告诉我哪里出错了?

感谢您的帮助!

【问题讨论】:

    标签: android uri android-imageview android-file


    【解决方案1】:

    您可以尝试以下方法:

    你可以试试Uri uri = Uri.parse(getFilesDir().getPath() + "/myimage.png"); ,而不是context.getFileStreamPath("myimage.png");

    【讨论】:

    • 您是否在清单中添加了<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>?位图是如何声明的?
    • WRITE_EXTERNAL_STORAGE 不是必需的,因为我使用内部存储进行读/写操作。对于位图,我首先打开画廊,然后选择图像时,我会在其上使用bitmapfactory.decodefile()。这部分工作正常。问题出在 ImageView 部分。
    • 好的,那就试试filepath.getPath();而不是filepath.toString()
    【解决方案2】:
    BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), getFileStreamPath("myimage.png").getAbsolutePath());
    
    imageView.setImageDrawable(bitmapDrawable);
    

    Bitmap b = BitmapFactory.decodeFile(getFileStreamPath("myimage.png").getAbsolutePath());
    

    【讨论】:

    • 位图 b = BitmapFactory.decodeFile(getFilesDir().getAbsolutePath() + "/myimage.png");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    相关资源
    最近更新 更多