从uri android加载图片

时间:2017-05-10 11:53:22

标签: java android imageview uri

我目前正在尝试将已保存到MySQLite数据库中的图像的Uri加载到RecyclerView中。

RecyclerViewAdapter的代码:

public void onBindViewHolder(MyCatchesAdapter.MyViewHolder holder, int position) {
    MyCatch myCatch = myCatchArrayList.get(position);
    holder.txtName.setText(myCatch.get_name());
    holder.txtLocation.setText(myCatch.get_location());
    holder.txtDate.setText(myCatch.get_date());

    holder.catchImage.setImageURI(Uri.parse(myCatch.get_catchImage()));
}

除图像外,所有数据都会加载到回收站视图中。我从Logcat

获得以下内容
Unable to open content: content://com.android.externalstorage.documents/document/primary%3ADownload%2Fsunrise.png

java.lang.SecurityException: Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider from ProcessRecord
{ee1a504 28074:com.example.pavlos.myproject/u0a74} (pid=28074, uid=10074) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS

我的清单权限是:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>

有什么东西我不见了吗?

编辑: 我如何得到Uri:

catchImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,
                    "Select Picture"), SELECT_PICTURE);
        }
    });

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            catchImage.setImageURI(selectedImageUri);
            imagePath = selectedImageUri;
            Log.d("imagePath","URI "+selectedImageUri);
        }
    }
}
public String getPath(Uri uri) {
    // just some safety built in
    if( uri == null ) {
        // TODO perform some logging or show user feedback
        return null;
    }
    // try to retrieve the image from the media store first
    // this will only work for images selected from gallery
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index);
        cursor.close();
        return path;
    }
    // this is our fallback here
    return uri.getPath();
}

2 个答案:

答案 0 :(得分:3)

首先,getPath()完全错误。充其量,它适用于MediaStore返回的某些图像子集。完全摆脱它。使用an image-loading library填充ImageView

就您的问题而言,只要该活动实例存在,您的活动就可以使用从Uri返回的ACTION_GET_CONTENT。当且仅当您包含Uri时,您才能将FLAG_GRANT_READ_URI_PERMISSION传递给其他组件(例如,其他活动)。您的流程终止后,您将失去访问该内容的所有权利。因此,请停止将Uri保留到数据库,或切换到ACTION_OPEN_DOCUMENT并获取可持久的Uri权限。

答案 1 :(得分:0)

看起来你正在获取图像URI(通常来自图库),它将作为内容URI(content://)。此内容URI带有临时权限,当它丢失当前上下文时会丢失。因此,下次加载URI时,您可能无权在当前上下文中加载它。

我建议您将图像的内容存储在数据库中,并在需要时加载它。可以应用诸如图像压缩的各种图像优化。您也可以查看此Glide库以进行Google推荐的图片管理。