簡體   English   中英

制作照片后,無法從相機接收Intent.extras()

[英]Can't receive Intent.extras() from Camera after making a photo

我發現這是捕捉照片和接收全尺寸照片而不是縮略圖的常見問題(根據: http//developer.android.com/training/camera/photobasics.html )。 拍攝照片和接收縮略圖很容易,但教程的其余部分似乎不完整而且無法正常工作。 有人輕松解決了嗎?

public class TakePhoto extends Activity{

static final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_TAKE_PHOTO = 1;
private String mCurrentPhotoPath;
private ImageView mImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.take_photo);

    mImageView = (ImageView) findViewById(R.id.imageView);

    dispatchTakePictureIntent();

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Log.i("ASD", ex.toString());
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

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 = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

}

04-25 18:01:14.239  17281-17281/com.(package).bazaar E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.(package).bazaar, PID: 17281
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.(package).bazaar/com.(package).bazaar.TakePhoto}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3626)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3669)
        at android.app.ActivityThread.access$1300(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at com.klangstudios.bazaar.TakePhoto.onActivityResult(TakePhoto.java:46)
        at android.app.Activity.dispatchActivityResult(Activity.java:6161)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3622)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3669)
            at android.app.ActivityThread.access$1300(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

我不希望那里的datanull 但是,該Intent不應該有任何額外內容。 如果指定EXTRA_OUTPUT則只查找"data" 如果您指定EXTRA_OUTPUTEXTRA_OUTPUT提供的路徑獲取照片,並忽略傳遞給onActivityResult()Intent

關於null data Intent本身,這可能是您正在使用的相機應用程序所特有的。 請記住,使用ACTION_IMAGE_CAPTURE意味着您依靠第三方應用程序來拍照,第三方應用程序可能存在錯誤。

這可能有所幫助: Android Camera Intent:如何獲得全尺寸照片?

雖然正在做的是在啟動相機意圖之前讀取您創建的文件,而不是讀取data

我在與“jean d'arme”相同的問題上掙扎了幾個小時,除了我想要獲取URI而不是Bitmap。 為了免除其他用戶的浪費時間,我將突出顯示“jean d'arme”和“Felipe Augusto”的說明,幫助我解決了錯誤,步驟如下:

1)在類中添加一個全局變量

private Uri uri;

2)使用此變量在dispatchTakePictureIntent()中獲取photoURI的內容

private void dispatchTakePictureIntent() {

        {...}

        if (photoFile != null) {
            uri = photoURI;
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

3)在onActivityResult方法中使用此Uri來執行您要執行的任何任務

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        //Perform any task using uri
        //For example set this URI to fill an ImageView like below
        this.imageView.setImageURI(uri);
    }
}

我希望這個亮點會幫助你們中的一些人!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM