2012-11-19 30 views
2

尝试阅读用户拍摄的照片列表并在imageview中显示它们。为了简单起见,现在试图只显示一个。有这样一段代码至今:在android中阅读用户照片

Cursor cc = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,null); 

这让我有些数据光标,cc.getCount()似乎正感(通过人去了,当我拍照等)。但是,我无法在imageView中显示内容,什么也没有显示出来。

Iv'e尝试这样做(S_ID是从上方光标的知情同意的ID,第一返回柱):

Uri u; 
u = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + s_id); 
im.setImageURI(u); 

还试图这样:

Bitmap b = BitmapFactory.decodeFile(u.getPath()); 
im.setImageBitmap(b); 

否worky。帮帮我?

ps。没有错误出现在任何地方。

+3

也许,[这会工作] – chRyNaN

+1

+1先前的评论。关键是你必须使用MediaStore.Images.Media.DATA字段。它有一个实际的图像文件的路径。 –

+0

aaaaand我觉得自己像个白痴。那就是诀窍,非常感谢你! – tsgandr

回答

1

下面是另一种让用户选择图像并将其显示在图像视图中的方法。

此代码将允许用户通过文件看起来从图库中选择一个图片:

Intent picture = new Intent(Intent.ACTION_GET_CONTENT); 
picture.setType("image/*"); 
startActivityForResult(picture, YOUR_CODE); 

然后在onActivityResult方法,你可以利用这些数据来设置ImageView的:

public void onActivityResult(int requestCode, int resultCode, Intent data){ 
    //This is where you can use the data from the previous intent to set the image view 
    Uri uri = data.getData(); 
    im.setImageURI(uri); //where im is your imageview 
} 

看看这个link for a more detailed answer.