使用持久数据管理本地图像
Managing local images with persistent data
我正在努力理解在 android 中关联图像和持久数据对象的简单解决方法是什么。更详细地说,我已经建立了一个简单的房间持久性架构,现在我需要向 java 持久化对象添加一个字段 "image"。我尝试使用 uri,但我对 Android 的了解非常少,我得到的是我在使用 android 文件管理器选择图像时恢复的 uri 仅在重新启动之前有效,所以如果我将如此获得的 uri 保存在数据库中,以后恢复时就没有意义了。我该如何管理?
基本上我需要的是一个简单的方法link一个对象到存储在phone中的本地图像(或者用相机在飞行中捕获),不用担心图像被删除用户或任何东西,只是一个简单的方法。
例如,我尝试修改 google 代码示例,但我显然失败了,因为我不知道我在做什么
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
ParcelFileDescriptor parcelFileDescriptor =
getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return image;
}
此代码导致编译错误,在调用 takePersistableUriPermission 时发现需要:parcedDescriptor... 和 VOID。我什至不知道这是否能解决我的问题。
这是我用来从本地图像获取 uri 的代码,但我还打算让相机拍摄照片并将其传递给 saving/linking 它
// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
// browser.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
// Filter to only show results that can be "opened", such as a
// file (as opposed to a list of contacts or timezones)
intent.addCategory(Intent.CATEGORY_OPENABLE);
// Filter to show only images, using the image MIME data type.
// If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
// To search for all documents available via installed storage providers,
// it would be "*/*".
intent.setType("image/*");
startActivityForResult(intent, READ_REQUEST_CODE);
the uri I recover when picking an image with the android file manager is only valid until reboot, so if I would save the so obtained uri in the database, it would make no sense when recovered later
这不太准确。
你通过 ACTION_OPEN_DOCUMENT
拉入的 Uri
对任何 activity 通过 onActivityResult()
获得 Uri
的东西都有好处。如果将 Uri
传递给另一个组件,则可以使用 FLAG_GRANT_READ_URI_PERMISSION
允许该组件读取 Uri
处的内容。但是一旦您的流程结束,您对该内容的访问权就会消失。
由于您使用了 ACTION_OPEN_DOCUMENT
,您可以使用 takePersistableUriPermission()
请求来长期访问内容,但这仍然只有在内容仍然存在的情况下才有效。如果用户删除了内容,甚至可能移动了内容,您将失去访问权限。
For istance I tried to tinker with the google code example but i clearly failed because I don't know what i'm doing
takePersistableUriPermission()
不 return 一个 ParcelFileDescriptor
。否则,那个特定的调用似乎没问题。
关于加载图像,请使用现有的图像加载库(例如,Glide、Picasso)。
我正在努力理解在 android 中关联图像和持久数据对象的简单解决方法是什么。更详细地说,我已经建立了一个简单的房间持久性架构,现在我需要向 java 持久化对象添加一个字段 "image"。我尝试使用 uri,但我对 Android 的了解非常少,我得到的是我在使用 android 文件管理器选择图像时恢复的 uri 仅在重新启动之前有效,所以如果我将如此获得的 uri 保存在数据库中,以后恢复时就没有意义了。我该如何管理?
基本上我需要的是一个简单的方法link一个对象到存储在phone中的本地图像(或者用相机在飞行中捕获),不用担心图像被删除用户或任何东西,只是一个简单的方法。
例如,我尝试修改 google 代码示例,但我显然失败了,因为我不知道我在做什么
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
ParcelFileDescriptor parcelFileDescriptor =
getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return image;
}
此代码导致编译错误,在调用 takePersistableUriPermission 时发现需要:parcedDescriptor... 和 VOID。我什至不知道这是否能解决我的问题。
这是我用来从本地图像获取 uri 的代码,但我还打算让相机拍摄照片并将其传递给 saving/linking 它
// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
// browser.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
// Filter to only show results that can be "opened", such as a
// file (as opposed to a list of contacts or timezones)
intent.addCategory(Intent.CATEGORY_OPENABLE);
// Filter to show only images, using the image MIME data type.
// If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
// To search for all documents available via installed storage providers,
// it would be "*/*".
intent.setType("image/*");
startActivityForResult(intent, READ_REQUEST_CODE);
the uri I recover when picking an image with the android file manager is only valid until reboot, so if I would save the so obtained uri in the database, it would make no sense when recovered later
这不太准确。
你通过 ACTION_OPEN_DOCUMENT
拉入的 Uri
对任何 activity 通过 onActivityResult()
获得 Uri
的东西都有好处。如果将 Uri
传递给另一个组件,则可以使用 FLAG_GRANT_READ_URI_PERMISSION
允许该组件读取 Uri
处的内容。但是一旦您的流程结束,您对该内容的访问权就会消失。
由于您使用了 ACTION_OPEN_DOCUMENT
,您可以使用 takePersistableUriPermission()
请求来长期访问内容,但这仍然只有在内容仍然存在的情况下才有效。如果用户删除了内容,甚至可能移动了内容,您将失去访问权限。
For istance I tried to tinker with the google code example but i clearly failed because I don't know what i'm doing
takePersistableUriPermission()
不 return 一个 ParcelFileDescriptor
。否则,那个特定的调用似乎没问题。
关于加载图像,请使用现有的图像加载库(例如,Glide、Picasso)。