显示从内部存储到imageview的图像

时间:2014-09-21 13:56:38

标签: android

我的图像位于内部存储中的myImageFactory文件夹下。现在我需要获取文件夹的路径,然后将其设置为imageviwer。我不能通过使用getFilesDir()bez我的图像不在文件文件夹中。现在我如何获取文件路径并分配给imageviwers setimageurl()。有什么方法可以将“路径”转换为Uri。我已经附加了下面的文件探索结构链接。

      public class GalleryImageAdapter extends BaseAdapter {
         ContextWrapper cw = new ContextWrapper(getApplicationContext());        
         File path = cw.getDir("myImageFactory", Context.MODE_WORLD_READABLE);           
         Uri[] picture={
             //How to get the path and store it here        
            };
            @Override
            public int getCount() {
                return picture.length;
            }
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView i = new ImageView(context);
                i.setImageURI(uri+ "/myimage.png");
                i.setLayoutParams(new Gallery.LayoutParams(200, 200));
                i.setScaleType(ImageView.ScaleType.FIT_XY);
                return i;
            }
        }
    }
}

http://i.stack.imgur.com/BtQnA.png

2 个答案:

答案 0 :(得分:0)

在文件路径之前添加file:/// ..

并确保您在清单

中拥有这些权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

  File file = new File ("yourpath");

    ImageView imageView = (ImageView) findViewById(R.id.imagview);

    imageView.setImageURI(Uri.fromFile(file));

答案 1 :(得分:0)

如果你想在Android应用程序中使用图像,你必须将它们全部放入你的'res'文件夹内的任何一个可绘制文件夹(例如: - res / drawable-hdpi)文件夹中。然后你可以通过这种方式访问​​它们:

imageView.setImageResource(R.drawable.myimage);

 String drawableName = "myimage";///provide the image's name alone excluding its extension
 imageView.setImageResource(getResId(drawableName));

在命名图像文件时,请遵循java变量命名约定。

其他方法是 1.使用'assets'文件夹和AssetsManager 2.使用'res / raw'文件夹。 有关详细信息,请访问以下链接

http://developer.android.com/guide/topics/resources/providing-resources.html

相关问题