尝试拍照时Android崩溃

时间:2016-07-25 15:52:35

标签: android

尝试创建一个Android应用程序。它将进入我的第二个活动,并在点击返回按钮时返回。但是我希望能够在第二个活动中拍照。当我单击按钮时,应用程序崩溃。

$( ".test" ).dialog({
          autoOpen: false,
          height:   "auto",
          width:    "auto",
          modal:    true,
          close:    function(e){
              $(this).destroy();
              // This returns wrong lenght
              console.log($('.test').length);
              if($('.test').length === 1) {
                        console.log($('.test a').text());
              }
          }  
});

以下是错误消息。

package com.example.gary.natureall;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.media.Image;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;


public class uploadPicScreen extends AppCompatActivity {
    ImageView ivCamera, ivUpload, ivGallery, ivImage;

    static final int REQUEST_IMAGE_CAPTURE = 144;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.upload_picture_layout);
        Intent activityThatCalled = getIntent();
        String previousActivity = activityThatCalled.getExtras().getString("callingActivity");
        TextView callingActivityMessage = (TextView) findViewById(R.id.calling_activity_info_text_view);
        callingActivityMessage.append(" "+ previousActivity);
        ImageView ivCamera = (ImageView) findViewById(R.id.ivCamera);
        ivImage = (ImageView) findViewById(R.id.ivImage);

if(! hasCamera())
    ivCamera.setEnabled(false);
    }
//Check if device has camera
    private boolean hasCamera(){
        return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
    }
//Launch the camera
    public void launchCamera(View view){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //Take a picture and pass the result to onActivityResult
        startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);

    }

    //If you want to return to the image captured


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
            //Get the photo
            Bundle extras = data.getExtras();
            Bitmap photo = (Bitmap) extras.get("data");
            ivImage.setImageBitmap(photo);

        }
    }

    public void onReturnBtnClick(View view) {
        Intent goingBack = new Intent();
        setResult(RESULT_OK, goingBack);
        finish();
    }
}

2 个答案:

答案 0 :(得分:1)

它看起来像是一个权限问题。 堆栈跟踪的相关部分是:

  

java.lang.SecurityException:Permission Denial:启动Intent {   ACT = android.media.action.IMAGE_CAPTURE   来自ProcessRecord {3ef341的cmp = com.sec.android.app.camera / .Camera}   9055:com.example.gary.natureall / u0a10}(pid = 9055,uid = 10010)with   撤销权限android.permission.CAMERA

你必须添加

<uses-permission android:name="android.permission.CAMERA" />

到您的清单,对于Android 6.0,您还必须在运行时处理权限 - 请参阅App crashes due to java.lang.SecurityException以获取示例

答案 1 :(得分:0)

感谢您的帮助。实际上通过删除清单中的权限请求来实现此功能。我寻求更多的帮助,看到了这个解决方案。给它一个去,它的工作。我不知道为什么这么做,但胜利才是胜利。如果这不是一个糟糕的参考,我就像斯蒂芬金的汤米诺克斯一样。