[英]Loading image from uri android
I am currently trying to load a Uri of an image i have saved into my MySQLite database into a RecyclerView. 我目前正在尝试将已保存到MySQLite数据库中的图像的Uri加载到RecyclerView中。
The code for the RecyclerViewAdapter : 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()));
}
All the data loads into the recycler view except the image. 除图像外,所有数据都加载到回收器视图中。 I get the following from the Logcat
我从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
And my Manifest permissions are : 我的清单权限是:
<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"/>
Is there something that i am missing? 有什么东西我不见了吗?
EDIT: How i get the Uri: 编辑:我如何得到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();
}
First, getPath()
is completely wrong. 首先,
getPath()
完全错误。 At best, it will work for some subset of images returned from the MediaStore
. 充其量,它适用于从
MediaStore
返回的某些图像子集。 Get rid of it entirely. 完全摆脱它。 Use an image-loading library to populate your
ImageView
. 使用图像加载库填充
ImageView
。
In terms of your problem, the Uri
that you get back from ACTION_GET_CONTENT
can be used by your activity, for as long as that activity instance is around. 就您的问题而言,只要该活动实例存在,您的活动就可以使用从
ACTION_GET_CONTENT
返回的Uri
。 You can pass that Uri
to other components (eg, another activity), if and only if you include FLAG_GRANT_READ_URI_PERMISSION
. 当且仅当您包含
FLAG_GRANT_READ_URI_PERMISSION
,您可以将该Uri
传递给其他组件(例如,另一个活动)。 Once your process terminates, you lose all rights to access the content. 您的流程终止后,您将失去访问该内容的所有权利。 So, stop persisting the
Uri
to a database, or switch to ACTION_OPEN_DOCUMENT
and take persistable Uri
permissions. 因此,停止将
Uri
持久保存到数据库,或切换到ACTION_OPEN_DOCUMENT
并获取可持久的Uri
权限。
Looks like you are getting the image URI (usually from gallery) which would come as a content URI (content://). 看起来您正在获取图像URI(通常来自图库),它将作为内容URI(content://)。 This content URI comes with temporary permission which would get lost when it loses the current context.
此内容URI带有临时权限,当它丢失当前上下文时会丢失。 So when you load the URI next time, you might not have permission to load it in the current context.
因此,下次加载URI时,您可能无权在当前上下文中加载它。
I would suggest you to store the content of the image in DB and load it whenever required. 我建议你将图像的内容存储在DB中,并在需要时加载它。 Various image optimizations like image compression can be applied.
可以应用诸如图像压缩的各种图像优化。 You may also look this Glide library for image management which is recommended by Google.
您也可以将此Glide库用于Google推荐的图像管理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.