【问题标题】:How to read image from internal memory in android?如何从android中的内存中读取图像?
【发布时间】:2015-09-18 05:22:51
【问题描述】:

这个我已经保存图像的内部存储器的屏幕截图。
我想在图像视图中读取图像。那么如何获取图像路径或如何从内存中读取图像? 请帮帮我..

我尝试了一些代码,但它不起作用:-

 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);

【问题讨论】:

  • 你想从图库中获取图片吗?
  • 您将图像路径保存在应用中的哪个位置? @tej
  • @mono :- 是的,在我的内部存储中。请查看 DDMS 的屏幕截图
  • @amarbir Singh:- 不..我希望图像显示在 imageview 的列表视图中
  • 您点击了哪个图片??然后获取要保存的图片的路径。

标签: android


【解决方案1】:
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 :打开失败:ENOENT(没有这样的文件或目录)07-01 03:35:54.131:W/System.err(6169):在 libcore.io.IoBridge.open(IoBridge.java:409)07-01 03: 35:54.131:W/System.err(6169):在 java.io.FileInputStream.(FileInputStream.java:78)
  • 你在哪里安装了应用程序,在外部存储器还是内部?
  • 尝试根据存储介质获取路径。
  • 但现在又出现内存不足的错误..我该如何解决这个问题
【解决方案2】:

这样试试就行了,

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);

【讨论】:

  • 日志猫的非图像显示错误是:-无法解码流:java.io.FileNotFoundException:/data/data/com.wtow.restaurant/app_imageDir/Restaurant/broccocasserole.jpg:打开失败: ENOENT(没有这样的文件或目录)
  • 所以你的图像保存在应用程序的本地内存中,然后路径保存在本地数据库中对吗?
  • 保存在数据库中的图像路径目录存在问题
  • /data/data/com.wtow.restaurant/app_files/Restaurant/broccocasserole.jpg- 这是您正确的图像路径。您正在保存为 /data/data/com.wtow.restaurant/app_imageDir/Restaurant/broccocasserole.jpg
  • 但现在又出现内存不足的错误..我该如何解决这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-07
相关资源
最近更新 更多