拍照时出现 FileUriExposedException

Posted

技术标签:

【中文标题】拍照时出现 FileUriExposedException【英文标题】:FileUriExposedException during Taking Photo 【发布时间】:2017-12-12 20:36:57 【问题描述】:

需要你的帮助。我正在编写一个简单的应用程序来拍照并将它们保存在智能手机的内部存储器中,因为我没有外部 microSD。当我运行应用程序并点击拍照按钮时,它崩溃了。我该如何处理?提前谢谢你。 以下是监控结果:

 java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                              at android.view.View.performClick(View.java:5647)
                                                                              at android.view.View$PerformClick.run(View.java:22462)
                                                                              at android.os.Handler.handleCallback(Handler.java:754)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:163)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6205)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
                                                                           Caused by: java.lang.reflect.InvocationTargetException
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                              at android.view.View.performClick(View.java:5647) 
                                                                              at android.view.View$PerformClick.run(View.java:22462) 
                                                                              at android.os.Handler.handleCallback(Handler.java:754) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                              at android.os.Looper.loop(Looper.java:163) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6205) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
                                                                           Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/AutoFare/Wed%20Dec%2013%2000%3A22%3A01%20GMT%2B04%3A00%202017.png exposed beyond app through ClipData.Item.getUri()
                                                                              at android.os.StrictMode.onFileUriExposed(StrictMode.java:1796)
                                                                              at android.net.Uri.checkFileUriExposed(Uri.java:2346)
                                                                              at android.content.ClipData.prepareToLeaveProcess(ClipData.java:845)
                                                                              at android.content.Intent.prepareToLeaveProcess(Intent.java:8957)
                                                                              at android.content.Intent.prepareToLeaveProcess(Intent.java:8942)
                                                                              at android.app.Instrumentation.execStartActivity(Instrumentation.java:1519)
                                                                              at android.app.Activity.startActivityForResult(Activity.java:4402)
                                                                              at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:54)
                                                                              at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
                                                                              at android.app.Activity.startActivityForResult(Activity.java:4360)
                                                                              at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:708)
                                                                              at com.example.emilhikmett.camera.MainActivity.Takepic(MainActivity.java:45)
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                              at android.view.View.performClick(View.java:5647) 
                                                                              at android.view.View$PerformClick.run(View.java:22462) 
                                                                              at android.os.Handler.handleCallback(Handler.java:754) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                              at android.os.Looper.loop(Looper.java:163) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6205) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 

MainActivity.java

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.util.Date;

public class MainActivity extends AppCompatActivity 
private static final int CAMERA_IMAGE_REQUEST = 101;
private String imageName;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportActionBar().hide();


public void Takepic(View view) 
    // Creating folders for Image
    Button one = (Button) findViewById(R.id.button16);
    String imageFolderPath = Environment.getExternalStorageDirectory().toString()
            + "/AutoFare";
    File imagesFolder = new File(imageFolderPath);
    imagesFolder.mkdirs();

    // Generating file name
    imageName = new Date().toString() + ".png";

    // Creating image here
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(imageFolderPath, imageName)));
    startActivityForResult(takePictureIntent,
            CAMERA_IMAGE_REQUEST);



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) 
// TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK && requestCode == CAMERA_IMAGE_REQUEST) 

        Toast.makeText(this, "Success",
                Toast.LENGTH_SHORT).show();
    


activity_main.xml

<LinearLayout
    android:layout_
    android:layout_
    tools:layout_editor_absoluteY="8dp"
    tools:layout_editor_absoluteX="8dp"
    android:orientation="vertical">
    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical"
        android:layout_weight="1"
        android:paddingLeft="100dp"
        android:paddingRight="100dp"
        android:paddingTop="40dp">

        <Button
            android:id="@+id/button16"
            android:layout_
            android:layout_
            android:text="take picture"
            android:textColor="#FFFFFF"
            android:textAlignment="textStart"
            android:layout_marginTop="100dp"
            android:padding="16dp"
            android:onClick="Takepic"
            android:clickable="true"
            />


    </LinearLayout>
    <LinearLayout
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingLeft="50dp"
        android:paddingRight="50dp"
        android:paddingBottom="30dp">
    </LinearLayout>



</LinearLayout>

Manifest 文件中的权限

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

【问题讨论】:

发布完整的 logcat...crash 已添加............ 【参考方案1】:

尝试添加:

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

Activity.onCreate()。所以VM会忽略文件URI暴露。

解决问题的第二种方法是改为创建Fileprovider

这是Good Read

最佳做法是设置FileProvider(您可以在链接上看到它)。

StrictMode 更像是一种以快捷方式解决问题的 hacky 方式。

【讨论】:

第一种方法是使用文件提供程序。最后一个是使用严格模式。不好的建议。 是的,我是这么认为的……我已经编辑了答案……谢谢顺便说一句@greenapps

以上是关于拍照时出现 FileUriExposedException的主要内容,如果未能解决你的问题,请参考以下文章

使用修改的 flashMode 属性拍照时出现 React-Native-Camera 错误

CV2:尝试拍照时出现“[WARN:0] terminating async callback”

发送图像时出现 Python 套接字错误。无法解码字节/意外 EOF

将照片保存到文件时出现问题

如何使用 CameraSource 拍照?

垃圾回收后立即尝试保存 PictureBox.Image 时出现 AccessViolationException