gpt4 book ai didi

android - 拍照后无法从相机接收 Intent.extras()

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:28:24 26 4
gpt4 key购买 nike

我发现拍摄照片和接收全尺寸照片而不是缩略图是一个常见问题(根据: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)

最佳答案

我不希望 data 在那里是 null。但是,该 Intent 不应有任何额外内容。如果您没有指定EXTRA_OUTPUT,您只需要寻找额外的“数据”。如果您指定 EXTRA_OUTPUT,您将从您在 EXTRA_OUTPUT 中提供的路径中获取照片,并忽略传递给 Intent >onActivityResult().

关于 null data Intent 本身,这可能是您正在使用的相机应用程序特有的东西。请记住,使用 ACTION_IMAGE_CAPTURE 意味着您依赖第三方应用拍照,而第三方应用可能存在错误。

关于android - 拍照后无法从相机接收 Intent.extras(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29875478/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com