Android 拍摄照片并保存到内部存储器,获取getUriForFile的错误
因此,我正在开发一个应用程序,其中我正在拍摄一张照片,并试图将其保存到应用程序的内部存储中。我与文件提供商有问题。我已经看了很多关于堆栈溢出的问题,但是如果可能的话,我想得到更详细的解释 我也遵循谷歌的例子,它给了我以下的错误 这是我每次学习谷歌的例子时都会用到的代码Android 拍摄照片并保存到内部存储器,获取getUriForFile的错误,android,android-camera,android-fileprovider,android-storage,Android,Android Camera,Android Fileprovider,Android Storage,因此,我正在开发一个应用程序,其中我正在拍摄一张照片,并试图将其保存到应用程序的内部存储中。我与文件提供商有问题。我已经看了很多关于堆栈溢出的问题,但是如果可能的话,我想得到更详细的解释 我也遵循谷歌的例子,它给了我以下的错误 这是我每次学习谷歌的例子时都会用到的代码 <provider android:name=".application.blinkupregistration.postworkphotos.PostWorkPhotosFileProvider"
<provider
android:name=".application.blinkupregistration.postworkphotos.PostWorkPhotosFileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
对于上面两个,我还尝试将com.myapp.provider硬编码到authorities和getUriForFile方法中。还为getUriForFile方法执行了getpackageName()。但这些变化不大。我认为主要的问题是路径
使用Google的示例尝试了以下路径
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="post_work_photos" path="Android/data/${applicationId}/files/Pictures" />
</paths>
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="post_work_photos" path="Android/data/com.myapp/files/Pictures" />
</paths>
每当我将paths.xml更改为以下内容时,我都可以使用它
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="post_work_photos" path="." />
</paths>
但我不明白为什么它在这个时期起作用。我也不知道这是否是正确的做法,这是我主要关心的问题
如果有人能帮我,那就太好了。谢谢
使用Google的示例尝试了以下路径
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="post_work_photos" path="Android/data/${applicationId}/files/Pictures" />
</paths>
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="post_work_photos" path="Android/data/com.myapp/files/Pictures" />
</paths>
他们的例子。。。有问题
但我不明白为什么它在这个时期起作用
因为您是说您愿意支持外部存储根中的任何内容
我也不知道这种做法是否正确
您可以使用
稍微缩小范围:
现在,您只能从文件指向的
getExternalFilesDir()
中提供文件。如果您试图将图片保存到应用程序的数据文件夹中,这应该可以:
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File(getApplicationContext().dataDir, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
从API级别4开始支持
为包的持久数据指定的默认目录的完整路径
否则,如果您在API级别24以上工作,则可以在上下文中的任何位置调用。(活动、服务等)
返回文件系统上存储使用openFileOutput(String,int)创建的文件的目录的绝对路径
Android的路径不是以正斜杠开始的吗
/Android/
@MarcosVasconcelos试图确保这一点,但这不是问题所在。谢谢你试过这个吗@正在将其保存到公用目录的Hamdroid。我想将其保存到应用程序存储中。@huey77请查看下面我的答案。谢谢回复。但出于某种原因,我没有理解你的意思——“因为你是说你愿意支持外部存储根目录中的任何内容。”抱歉,可能只是有一个缓慢的早晨。@huey77:
映射到所有外部存储根目录(即环境中的所有内容。getExternalStorageDirectory()
仅映射到与应用程序绑定的外部存储上的特定目录。哦,这很有意义。非常感谢您的澄清!
<external-files-path name="post_work_photos" path="." />
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File(getApplicationContext().dataDir, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);