有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在安卓中读取用户照片

正在尝试读取用户拍摄的照片列表并将其显示在imageview中。为了简单起见,现在尝试只显示一个。到目前为止,我们已经有了这段代码:

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

没有工作。帮忙

注:任何地方都不会出现错误


共 (1) 个答案

  1. # 1 楼答案

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

    此代码允许用户浏览文件,从图库中选择图像:

    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.