gpt4 book ai didi

android - 为什么以及如何在 Android Studio 中使用 @xml/file_paths 资源?

转载 作者:行者123 更新时间:2023-12-03 17:34:17 40 4
gpt4 key购买 nike

tl;博士:当我在运行时动态检索 file_path 时,为什么需要 file_paths 资源?

我目前正在通过构建自己的应用程序来学习 Android Studio。
我现在想做的是拍张照片,然后从中取出一些文字。
拍张照以后用,我跟着Taking Photos Simply Android 开发者指南。此时,建议使用 File Provider .

在 list 中,文件提供程序是这样添加的:

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

我正在为 meta-data 苦苦挣扎,尤其是 android:resource=@xml/file_paths"
在“简单拍照教程”中,需要对 XML 进行添加才能使其正常工作:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />
</paths>

所以我的问题是:我应该遵循的开发人员指南中的良好实践是什么,我该怎么做?

编辑:为了让事情更清楚,我根本无法理解如何在这里使用资源。
这是指南中的方法代码:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);

// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}

所以我在我的应用程序的运行时动态地获取文件的名称(因此它是文件路径)。
那个文件/路径是什么,我不明白它的用法。

最佳答案

中创建一个文件夹资源 目录并将其命名为 xml , 创建文件文件路径 在其中并用以下内容替换所有内容:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
</paths>
如果您想从该教程中预览捕获的图像,则将文件从其路径解码为位图,然后将其显示到 的 ImageView 上。 onActivityResult()
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val bitmap=BitmapFactory.decodeFile(currentPhotoPath)
imageView?.setImageBitmap(bitmap)
}
}

关于android - 为什么以及如何在 Android Studio 中使用 @xml/file_paths 资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48598711/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com