Camera_intent已停止 [英] Camera_intent has stopped

查看:66
本文介绍了Camera_intent已停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的新手,最近正在开发我的第一个应用程序,该应用程序基本上会启动本机摄像头应用程序,拍摄照片并将其保存在设备上.但是,当我尝试启动相机应用程序时,该应用程序始终崩溃.请帮助我进行排序.谢谢.

I'm a novice in android and was recently developing my first app which basically launches the native camera application, takes a picture and saves it on the device. However, the app keeps crashing when I try to launch the camera app. Please help me get this sorted. Thanks.

logcat的日志:

The log from 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文件包含:

The MainActivity file contains:

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文件中添加了权限:

I've also added the permissions in the manifest.xml file:

<?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>

推荐答案

首先,当您单击按钮拍照时:

first when you click on button to take picture:

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结果中:

Now in OnActivity Result:

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的文件:

You have to make folder named xml inside res and create file named provider_paths.xml :

<?xml version="1.0" encoding="utf-8"?>
<paths >
    <external-path name="/storage/emulated/0/" path="."/>
</paths>

现在,以明显的方式显示:

Now, in menifest put:

 <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);
    }

这篇关于Camera_intent已停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆