我已经搜索了很多来解决这个问题,但无法解决这个问题。
我在我的应用程序中使用相机功能,并且将意图传递给回调方法时遇到了麻烦。用户可以从图库中上传图像
retrieveFromGallery()
工作正常。他也可以拍张照片
takePicture()
导致
崩溃onActivityResult()
因为意图为null。但是,当我打电话
时,它不是空的startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
图片保存在定义的文件路径中,但是我想在应用程序中显示它,就像我从图库中检索它一样。 我按照Android Developer Tutorial,所以我的代码应该与那里相同。唯一区别:我使用片段 非常感谢任何帮助:)
private static final int PICK_IMAGE_REQUEST = 2;
private static final int REQUEST_TAKE_PHOTO = 3;
public void retrieveFromGallery() {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
public void takePicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(this.getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
Log.d(LOG_TAG, "creating photofile: " + photoFile);
} catch (IOException ex) {
// Error occurred while creating the File
Log.d(LOG_TAG, "error creating the file" + ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
Log.d(LOG_TAG, "temporary photofile path: " + photoFile);
takePictureIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri photoURI = FileProvider.getUriForFile(this.getActivity(),
getActivity().getApplicationContext().getPackageName() + ".fileprovider", photoFile);
Log.d(LOG_TAG, "uri: " + photoURI);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
Log.d(LOG_TAG, "intent: " + takePictureIntent + " , " + REQUEST_TAKE_PHOTO);
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getActivity().getContentResolver(), uri);
popupWindowSelectUploadSource.dismiss();
selectedImage.setVisibility(View.VISIBLE);
selectedImage.setImageBitmap(bitmap);
Log.d(LOG_TAG, "img path" + bitmap);
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Log.d(LOG_TAG, "on activity result - intent: " + requestCode + "resultcode "+ resultCode + "data " + data);
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
popupWindowSelectUploadSource.dismiss();
selectedImage.setImageBitmap(imageBitmap);
}
}
来自我的Logcat:
03-18 13:26:36.752 593-593/com.example.myapp D/SpotMap: opening a popup window
03-18 13:26:37.721 593-593/com.example.myapp D/SpotMap: opening the popup window to select upload source
03-18 13:26:38.626 593-593/com.example.myapp D/SpotMap: creating photofile:/storage/emulated/0/Android/data/com.example.myapp/files/Pictures/JPEG_20180318_132638_5713532469653547292.jpg
03-18 13:26:38.627 593-593/com.example.myapp D/SpotMap: temporary photofile: path: /storage/emulated/0/Android/data/com.example.myapp/files/Pictures/JPEG_20180318_132638_5713532469653547292.jpg
03-18 13:26:38.627 593-593/com.example.myapp D/SpotMap: uri: content://com.example.myapp.fileprovider/my_images/JPEG_20180318_132638_5713532469653547292.jpg
03-18 13:26:38.681 593-593/com.example.myapp D/SpotMap: intent: Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 clip={text/uri-list U:content://com.example.myapp.fileprovider/my_images/JPEG_20180318_132638_5713532469653547292.jpg} (has extras) } , 3
03-18 13:26:38.714 593-593/com.example.myapp D/SpotMap: on Pause
03-18 13:26:47.444 593-593/com.example.myapp D/SpotMap: on activity result - intent: 3resultcode -1data null
我的错误讯息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapp, PID: 593
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65539, result=-1, data=null} to activity {com.example.vreeni.StreetMovementApp/com.example.myapp.MainActivity}: 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:4339)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4382)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1654)
更新:
我尝试了类似帖子中的建议,我能够进入OnActivityResult方法,并在那里创建URI。但是,然而,它无法转换为Bitmap。返回null。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getActivity().getContentResolver(), uri);
popupWindowSelectUploadSource.dismiss();
selectedImage.setVisibility(View.VISIBLE);
selectedImage.setImageBitmap(bitmap);
Log.d(LOG_TAG, "img path" + bitmap);
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
File photoFile = null;
try {
photoFile = createImageFile();
Log.d(LOG_TAG, "creating photofile: " + photoFile);
} catch (IOException ex) {
// Error occurred while creating the File
Log.d(LOG_TAG, "error creating the file" + ex);
}
if (photoFile != null) {
Log.d(LOG_TAG, "temporary photofile path: " + photoFile);
Uri photoURI = FileProvider.getUriForFile(this.getActivity(),
getActivity().getApplicationContext().getPackageName() + ".fileprovider", photoFile);
Log.d(LOG_TAG, "uri: " + photoURI);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getActivity().getContentResolver(), photoURI);
Log.d(LOG_TAG, "bitmap: " + bitmap);
selectedImage.setVisibility(View.VISIBLE);
selectedImage.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}