【问题标题】:Intent camera crash故意相机崩溃
【发布时间】:2018-04-23 22:46:32
【问题描述】:

我现在正在制作一个简单的安卓应用。它只是允许用户拍照然后显示它。 当我在虚拟设备中测试它时,没关系。但是,当我将 apk 下载到我的 android 设备时,在我用后置相机拍照后,应用程序已停止并返回主菜单。只是后置摄像头的问题。 另外,在虚拟设备中,拍照后,照片会显示成功。但我手机里是空的

在 MainActivity 中,我单击“拍照”,它将启动相机并将我的图像复制到文件夹中。然后它将Photo的路径发送到下一个活动并显示它。

这是我的 MainActivity

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_ID_IMAGE_CAPTURE = 100;
    Button TakePhoto, InsertPhoto, Exit;
    String mCurrentPhotoPath;

    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 = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(imageFileName,".jpg",storageDir);


        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TakePhoto = (Button) findViewById(R.id.button);
        InsertPhoto = (Button) findViewById(R.id.button3);
        Exit = (Button) findViewById(R.id.button2);
        //Start Camera
        TakePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (intent.resolveActivity(getPackageManager()) != null) {
                    // Create the File where the photo should go
                    File photoFile = null;
                    try {
                        photoFile = createImageFile();
                    } catch (IOException ex) {

                        System.out.println(ex);
                    }
                    // Continue only if the File was successfully created
                    if (photoFile != null) {
                        Uri photoURI = Uri.fromFile(photoFile);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                        startActivityForResult(intent,REQUEST_ID_IMAGE_CAPTURE);
                    }

                }

            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ID_IMAGE_CAPTURE) {
            if (resultCode == RESULT_OK) {
                //Bitmap bp = (Bitmap)data.getExtras().get("data");
                //ByteArrayOutputStream stream = new ByteArrayOutputStream();
               // bp.compress(Bitmap.CompressFormat.PNG, 100, stream);
               // byte[] images = stream.toByteArray();
                File imgFile = new  File(mCurrentPhotoPath);
                if(imgFile.exists()){
                    System.out.println("This is file"+mCurrentPhotoPath.toString());
                    Intent Show = new Intent(MainActivity.this, ShowPhoto.class);

                    Show.putExtra("image",imgFile);

                    startActivity(Show);
                }



            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "Action canceled", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "Action Failed", Toast.LENGTH_LONG).show();
            }
        }

    }
}

这是 ShowPhoto 活动

public class ShowPhoto extends Activity {
private LinearLayout Image;
Button Back,Next;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_photo);
    Image=(LinearLayout)findViewById(R.id.linearLayout);
    //Get Image from previous Activity
    File image = (File)getIntent().getExtras().get("image");
    Bitmap bmp = BitmapFactory.decodeFile(image.getAbsolutePath());

    ImageView imageView = new ImageView(getApplicationContext());

    imageView.setImageBitmap(Bitmap.createScaledBitmap(bmp, bmp.getWidth()*2, bmp.getHeight()*2, true));

    Image.addView(imageView);





}

}

这是我运行应用程序时 logcat 中的内容 enter image description here

enter image description here

【问题讨论】:

  • 注意:棉花糖及以上? ,您需要运行时权限并发布 logcat 详细信息
  • 我选择 Lolipop 作为虚拟设备,我的手机是 Galaxy j5 2016。我是 android 编程新手。
  • 如果您发布错误堆栈跟踪,则很容易识别问题。
  • 是的,它需要运行时权限

标签: java android android-intent camera


【解决方案1】:

使用时应在运行时请求权限并在Manifest中声明权限

  if (ContextCompat.checkSelfPermission(getContext(),
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale((Activity)
                getContext(), Manifest.permission.CAMERA)) {


        } else {
            ActivityCompat.requestPermissions((Activity) getContext(), 
                    new String[]{Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_CAMERA);
        }

    }

为了你的清单

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

【讨论】:

  • 顺便说一下,有时 Studio 会向您的 Activity 导入错误的 Manifest。如果遇到问题,只需将 android.Manifest.permission.CAMERA 放在开头即可
  • 我添加了权限,但它不起作用。在 Android 6.0.1 中,当我用后置摄像头拍照时,应用程序已停止。照片仍然可用并保存在应用的文件夹中
  • @NgọcAnh 好的,如果添加权限对您不起作用,请编辑您的问题并为我们放置日志以查看实际错误是什么。崩溃可能还有其他原因
  • 我已经添加了 LogCat 的图像,我不知道这是否是你需要的。请帮我。非常感谢
  • @NgọcAnh 你的错误应该是这样的google.ru/…: 你有类似的词“错误”或“异常”吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-16
  • 1970-01-01
  • 2018-05-07
  • 2012-03-11
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
相关资源
最近更新 更多