如何在Android中从内部存储器读取图像?

6

这是我内部存储器的屏幕截图,我已经保存了图片。
我想在图片视图中查看图片。那么如何获取图片路径或者如何从内部存储器中读取图片呢?
请帮帮我。

我尝试了一些代码,但它们并没有起作用:

 FileInputStream fis;
        String filePath = activity.getFilesDir().getPath();
         String path = data.get(position).get("product_image");
         fis = openFileInput(filePath+"/broccolicasserole.jpg");
         Bitmap      bitmapA = BitmapFactory.decodeStream(fis);
         producticon.setImageBitmap(bitmapA);

从数据库获取图像名称的方式如下:-data.get(position).get("product_image");
还可以尝试以下代码:

  >  String filePath = activity.getFilesDir().getPath();
   File filePath1 =   imageLoader.DisplayImage(filePath+data.get(position).get("product_image"), producticon);

this is my internal memory which i save that image already


你在应用程序中保存图像路径的位置在哪里?@tej - Android Dev
@mono:- 是的,在我的内部存储中。请检查DDMS的屏幕截图。 - tej shah
1
@amarbir Singh:不是的,我想要在ListView中的ImageView中显示图片。 - tej shah
我在你的其他评论中看到,你遇到了一个错误:/data/data/com.wtow.restaurant/app_imageDir/Restaurant/broccolicasserole.jpg,请检查你问题中的图片。你的路径应该是/data/data/com.wtow.restaurant/app_files/Restaurant/broccolicasserole.jpg - Knossos
在Stackoverflow上搜索。那里有大量的文档资料。Android在加载图像方面有众所周知的问题,主要是由于应用程序的内存限制。 - Knossos
显示剩余4条评论
2个回答

7
private String readFileFromInternalStorage(){
    ImageView imageView = (ImageView) findViewById(R.id.image);
    ContextWrapper cw = new ContextWrapper(context);

    //path to /data/data/yourapp/app_data/dirName
    File directory = cw.getDir("dirName", Context.MODE_PRIVATE);          
    File mypath=new File(directory,"imagename.jpg");

    ImageView imageView = (ImageView) findViewById(R.id.image);
    imageView.setImageDrawable(Drawable.createFromPath(mypath.toString()));
}

代码未经测试。或者尝试以下方法!

private void setImage(String imgPath)
{

    try {
        File f=new File(imgPath, "imgName.jpg");
        Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
        ImageView img=(ImageView)findViewById(R.id.image);
        img.setImageBitmap(b);
    } 
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }

}

只需尝试这两个代码。没有得到图像错误,类似于:- 07-01 03:35:54.131: W/System.err(6169): java.io.FileNotFoundException: /data/data/com.wtow.restaurant/app_imageDir/Restaurant/broccolicasserole.jpg: open failed: ENOENT (No such file or directory) 07-01 03:35:54.131: W/System.err(6169): at libcore.io.IoBridge.open(IoBridge.java:409) 07-01 03:35:54.131: W/System.err(6169): at java.io.FileInputStream.<init>(FileInputStream.java:78) - tej shah
你安装这个应用程序是在外部存储器还是内部? - Anoop M Maddasseri
尝试根据存储介质获取路径。 - Anoop M Maddasseri
但现在又出现了内存不足的错误..我该如何解决? - tej shah
请查看https://dev59.com/i-o6XIcBkEYKwwoYGwMZ#29863404。 - Anoop M Maddasseri
让我们在聊天中继续这个讨论 - tej shah

1
尝试像这样,它会工作:

尝试像这样,它会工作,

ImageView  picture = (ImageView) findViewById(R.id.imageView1);  

       String pic = result.getString(YourImagepathname);//get path of your image
        Bitmap yourSelectedImage1 = BitmapFactory.decodeFile(pic);
       picture.setImageBitmap(yourSelectedImage1);

日志cat的图像显示错误是: - 无法解码流: java.io.FileNotFoundException: /data/data/com.wtow.restaurant/app_imageDir/Restaurant/broccolicasserole.jpg: 打开失败: ENOENT(文件或目录不存在) - tej shah
所以你的图像保存在应用程序的本地内存中,然后路径保存在本地数据库中,对吗? - Android Dev
你的图像路径目录在保存到数据库时出现问题。 - Android Dev
/data/data/com.wtow.restaurant/app_files/Restaurant/broccolicasserole.jpg- 这是您的正确图像路径。您正在保存为/data/data/com.wtow.restaurant/app_imageDir/Restaurant/broccolicasserole.jpg。 - Android Dev
但现在又出现了内存不足的错误..我该如何解决? - tej shah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接