拍照期间的FileUriExposedException

时间:2017-12-12 20:36:58

标签: android image camera save

需要你的帮助。我正在编写一个简单的应用程序,用于拍照并将其保存在智能手机的内部存储器中,因为我没有外部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_width="368dp"
    android:layout_height="495dp"
    tools:layout_editor_absoluteY="8dp"
    tools:layout_editor_absoluteX="8dp"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1"
        android:paddingLeft="100dp"
        android:paddingRight="100dp"
        android:paddingTop="40dp">

        <Button
            android:id="@+id/button16"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            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_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingLeft="50dp"
        android:paddingRight="50dp"
        android:paddingBottom="30dp">
    </LinearLayout>



</LinearLayout>

清单文件中的权限

<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" />

1 个答案:

答案 0 :(得分:1)

尝试添加:

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Activity.onCreate()中的

。因此,VM会忽略文件URI暴露。

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

这是Good Read

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

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