InD*_*ond 9 android android-resources android-studio
tl; dr:当我在运行时动态检索 file_path 时,为什么需要 file_paths 资源?
我目前正在通过构建自己的应用程序来学习 Android Studio。我现在想做的是拍一张照片,然后从中取出一些文本。为了拍照并稍后使用,我遵循了 Android 开发人员的简单拍照指南。此时,建议使用File Provider。
在清单中,文件提供程序添加如下:
<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>
Run Code Online (Sandbox Code Playgroud)
我正在与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>
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:我应该遵循的开发人员指南中的良好做法是什么,我该怎么做?
编辑:为了让事情更清楚,我根本无法理解如何在此处使用资源。这是本指南中的方法代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
所以我在我的应用程序的运行时动态获取文件的名称(因此它是文件路径)。那个文件/路径是什么,我不明白它的用法。
在res目录中创建一个文件夹并将其命名为xml,在其中创建一个文件file_paths并将所有内容替换为:
<?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>
Run Code Online (Sandbox Code Playgroud)
如果您想预览该教程中捕获的图像,请将文件从其路径解码为位图,然后通过onActivityResult()将其显示到 ImageView 上
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)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6389 次 |
| 最近记录: |