如何在Android的ACTION_IMAGE_CAPTURE上处理第三方相机应用程序?

时间:2016-02-02 11:25:17

标签: android camera

我开始ACTION_IMAGE_CAPTURE意图启动相机标题。

pictureActionIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (pictureActionIntent.resolveActivity(getPackageManager()) != null) {
    File photoFile = null;
    try {
        photoFile = createImageFile();
    } catch (IOException ex) {
        e.printStackTrace();
    }
    if (photoFile != null) {
        pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
    }
}
startActivityForResult(pictureActionIntent, CAMERA_REQUEST);

如果使用默认的相机应用程序,则没有问题。 但是,如果在选择器对话框中选择了第三方相机应用程序,我该如何处理意图?

如果我使用例如CandyCamera没有任何反应。 通常,捕获的图像将显示在ImageView中。如果选择CandyCamera并拍摄照片,则不会发生任何事情。

示例:

switch (requestCode) {
        case CAMERA_REQUEST:
            if (resultCode == RESULT_OK && null != data) {

                galleryAddPic();
                setPic();

                break;
            }

createImageFile()中使用的photoFile = createImageFile();

    private File createImageFile() throws IOException {

    Context context = getApplicationContext();

    String uuid = items.get("uuid");

    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            uuid, //prefix
            ".jpg",        //suffix
            storageDir     //directory
    );


    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

这是setPic():

private void setPic() {

int targetW = 150;
int targetH = 150;

// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
try {
    InputStream in = getContentResolver().openInputStream(Uri.parse(mCurrentPhotoPath));
    BitmapFactory.decodeStream(in, null, bmOptions);
} catch (FileNotFoundException e) {
    Log.d("In setPic:", " konnte Datei nicht finden.");
}

//BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;

// Determine how much to scale down the image
int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = null;

try {
    InputStream in = getContentResolver().openInputStream(Uri.parse(mCurrentPhotoPath));
    bitmap = BitmapFactory.decodeStream(in, null, bmOptions);
    saveImageInXML saveImageInXML = new saveImageInXML();
    saveImageInXML.execute();
} catch (FileNotFoundException e) {
    Log.d("In setPic:", " konnte Datei nicht finden.");
}

Image1.setImageBitmap(bitmap);

}

那么,处理这个问题的正确方法是什么?

感谢您的帮助!

亲切的问候!

0 个答案:

没有答案