我是Android的新手,最近正在开发我的第一个应用程序,它基本上启动了原生相机应用程序,拍照并将其保存在设备上。但是,当我尝试启动相机应用程序时,应用程序不断崩溃。请帮我把它整理好。感谢。
来自logcat的日志:
03-22 15:13:35.143: D/AndroidRuntime(17395): Shutting down VM
03-22 15:13:35.144: E/AndroidRuntime(17395): FATAL EXCEPTION: main
03-22 15:13:35.144: E/AndroidRuntime(17395): Process: com.example.vivek.camera_intent, PID: 17395
03-22 15:13:35.144: E/AndroidRuntime(17395): java.lang.IllegalStateException: Could not execute method for android:onClick
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.view.View.performClick(View.java:5612)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.view.View$PerformClick.run(View.java:22285)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.os.Handler.handleCallback(Handler.java:751)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.os.Handler.dispatchMessage(Handler.java:95)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.os.Looper.loop(Looper.java:154)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.app.ActivityThread.main(ActivityThread.java:6123)
03-22 15:13:35.144: E/AndroidRuntime(17395): at java.lang.reflect.Method.invoke(Native Method)
03-22 15:13:35.144: E/AndroidRuntime(17395): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
03-22 15:13:35.144: E/AndroidRuntime(17395): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
03-22 15:13:35.144: E/AndroidRuntime(17395): Caused by: java.lang.reflect.InvocationTargetException
03-22 15:13:35.144: E/AndroidRuntime(17395): at java.lang.reflect.Method.invoke(Native Method)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
03-22 15:13:35.144: E/AndroidRuntime(17395): ... 9 more
03-22 15:13:35.144: E/AndroidRuntime(17395): Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/I_20170322_151335_1517517949.jpg exposed beyond app through ClipData.Item.getUri()v
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.os.StrictMode.onFileUriExposed(StrictMode.java:1813)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.net.Uri.checkFileUriExposed(Uri.java:2360)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.content.ClipData.prepareToLeaveProcess(ClipData.java:832)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.content.Intent.prepareToLeaveProcess(Intent.java:8957)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.content.Intent.prepareToLeaveProcess(Intent.java:8942)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1583)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.app.Activity.startActivityForResult(Activity.java:4228)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.app.Activity.startActivityForResult(Activity.java:4187)
03-22 15:13:35.144: E/AndroidRuntime(17395): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
03-22 15:13:35.144: E/AndroidRuntime(17395): at com.example.vivek.camera_intent.CameraIntentActivity.callCameraApp(CameraIntentActivity.java:102)
03-22 15:13:35.144: E/AndroidRuntime(17395): at com.example.vivek.camera_intent.CameraIntentActivity.takePhoto(CameraIntentActivity.java:50)
03-22 15:13:35.144: E/AndroidRuntime(17395): ... 11 more
MainActivity文件包含:
package com.example.vivek.camera_intent;
import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CameraIntentActivity extends AppCompatActivity {
private static final int REQUEST_IMAGE_CAPTURE = 911;
private ImageView mImageView;
private String mCurrentPhotoPath;
private static final int REQUEST_EXTERNAL_STORAGE_RESULT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_intent);
mImageView = (ImageView) findViewById(R.id.imageView);
}
@TargetApi(Build.VERSION_CODES.M)
public void takePhoto(View view)
{
//Toast.makeText(this, "Camera Button Clicked", Toast.LENGTH_SHORT).show();
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
{
callCameraApp();
}
else
{
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE))
{
Toast.makeText(this, "Need external storage permission", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE_RESULT);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
if (requestCode == REQUEST_EXTERNAL_STORAGE_RESULT)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
callCameraApp();
}
else
{
Toast.makeText(this, "Request permission not granted", Toast.LENGTH_SHORT).show();
}
}
else
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void callCameraApp()
{
Intent takePictureIntent = new Intent();
takePictureIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try
{
photoFile = createImageFile();
}
catch (IOException ex)
{
ex.printStackTrace();
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{
//Toast.makeText(this, "Photograph Captured Successfully", Toast.LENGTH_SHORT).show();
//Bundle extras = data.getExtras();
//Bitmap imageBitmap = (Bitmap) extras.get("data");
//mImageView.setImageBitmap(imageBitmap);
Bitmap imageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
mImageView.setImageBitmap(imageBitmap);
}
}
File createImageFile() throws IOException
{
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "I_" + timestamp + "_";
File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, ".jpg", storageDirectory);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
}
我还在manifest.xml文件中添加了权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vivek.camera_intent">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-sdk android:minSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".CameraIntentActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
首先点击按钮拍照:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (Exception ex) {
ex.printStackTrace();
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(getApplicationContext(),
BuildConfig.APPLICATION_ID + ".provider",
photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(intent, 1);
}
}
方法:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = new File(Environment.getExternalStorageDirectory(), "FOLDER_NAME");
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = image.getPath();
return image;
}
现在在OnActivity结果中:
try {
Uri takenPhotoUri = getPhotoFileUri(mCurrentPhotoPath);
new ProcessIMG().execute(takenPhotoUri.getPath());
} catch (Exception e) {
e.printStackTrace();
}
方法:
public Uri getPhotoFileUri(String fileName) {
if (isExternalStorageAvailable()) {
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "FOLDER_NAME");
if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) {
Log.d("GymT", "failed to create directory");
}
return FileProvider.getUriForFile(getApplicationContext(),
BuildConfig.APPLICATION_ID + ".provider",
new File(fileName));
}
return null;
}
您必须在res中创建名为xml的文件夹并创建名为provider_paths.xml的文件:
<?xml version="1.0" encoding="utf-8"?>
<paths >
<external-path name="/storage/emulated/0/" path="."/>
</paths>
现在,在清单中:
<provider
android:name="android.support.v4.content.FileProvider"
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>
private boolean isExternalStorageAvailable() {
String state = Environment.getExternalStorageState();
return state.equals(Environment.MEDIA_MOUNTED);
}