我正在尝试在设备的外部存储目录中存储一个图像(用相机捕获的图像)。我遵循了这一点:https://developer.android.com/reference/androidx/core/content/fileprovider.
但在按按钮拍照时有个问题。
这里是logcat的输出:
java.lang.IllegalStateException: Could not execute method for android:onClick
...
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.finalproject/files/Pictures/JPEG_20201229_115929_7049104839298740054.jpg
...
这是我的派送图片功能:
public void dispatchTakePictureIntent (View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File imageFile=null;
imageFile=createImageFile();
if (imageFile!=null){
pathToFile= imageFile.getAbsolutePath();
Uri photoUri= FileProvider.getUriForFile(MainActivity.this,"com.example.finalproject.fileprovider",imageFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
这是我的android manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.finalproject">
<uses-feature android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:theme="@style/Theme.FinalProject"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.finalproject.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
</manifest>
主要活动:
<Button
android:id="@+id/button"
android:layout_width="117dp"
android:layout_height="77dp"
android:onClick="dispatchTakePictureIntent"
android:text="camera"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.995" />
最后,文件路径:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Android/data/com.example.finalproject/files/Pictures" />
</paths>
imagefile.getabsolutepath()是:/storage/emulated/0/android/data/com.example.finalproject/files/pictures/jpeg\u 20201230\u 095104\u 22370060963499233.jpg
我已经尝试解决这个问题很长时间了,所以如果有人能帮助我,我会非常感激。
p、 d:如果需要更多的信息或代码,请留言,我会上传。
暂无答案!
目前还没有任何答案,快来回答吧!