gpt4 book ai didi

android - 拍照时出现 FileUriExposedException

转载 作者:行者123 更新时间:2023-11-30 00:13:18 26 4
gpt4 key购买 nike

需要你的帮助。我正在编写一个简单的应用程序来拍照并将它们保存在智能手机的内部存储器中,因为我没有外部 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) 

主 Activity .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>

list 文件中的权限

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

最佳答案

尝试添加:

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

Activity.onCreate() 中。因此 VM 会忽略文件 URI 暴露。

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

这是一个Good Read

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

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

关于android - 拍照时出现 FileUriExposedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47781132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com