我正在尝试获取我传递给intent的位图文件,但getParcelable()方法每次都返回null。
以下是我发送位图图像的java代码:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = ImageSharing.getOutputMediaFileUri(StaticMembers.MEDIA_TYPE_IMAGE, false);
List<Intent> cameraIntents = new ArrayList<Intent>();
PackageManager packageManager = getActivity().getPackageManager();
List<ResolveInfo> listCam = packageManager.queryIntentActivities(cameraIntent, 0);
for (ResolveInfo res : listCam) {
String packageName = res.activityInfo.packageName;
Intent intent = new Intent(cameraIntent);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
intent.setPackage(packageName);
intent.putExtra("return-data", true);
cameraIntents.add(intent);
}
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType(StaticMembers.IMAGE_TYPE);
Intent chooserIntent;
if (null == newmobilelangs && null == newmobilelangs.getCommon().getCompleteAction()) {
chooserIntent = Intent.createChooser(galleryIntent, "Complete action using");
} else {
chooserIntent = Intent.createChooser(galleryIntent, newmobilelangs.getCommon()
.getCompleteAction());
}
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, 1);
下面是我试图检索图像的java代码:
Bitmap profilePic;
String fileName, filePath;
Log.i("what is data?", data.toString());
Bundle extras = data.getExtras();
profilePic = extras.getParcelable("data");
上面代码的最后一行出现问题:
profilePic = extras.getParcelable("data");
以下是来自SettingFragment.java的onActivityResult()方法
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1) {
boolean isCamera;
isCamera = ((data == null) || (data.hasExtra(MediaStore.EXTRA_OUTPUT)));
Intent intent = new Intent("com.android.camera.action.CROP");
if (isCamera) {
intent.setDataAndType(fileUri, StaticMembers.IMAGE_TYPE);
} else {
intent.setDataAndType(data.getData(), StaticMembers.IMAGE_TYPE);
}
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// intent.putExtra("outputX", 150);
// intent.putExtra("outputY", 150);
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
tempFileURI = ImageSharing.getOutputMediaFileUri(StaticMembers.MEDIA_TYPE_IMAGE, false);
Log.i("Yes!", tempFileURI.toString());
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempFileURI);
startActivityForResult(intent, 2);
} else if (requestCode == 2) {
Bitmap profilePic;
String fileName, filePath;
Log.i("what is data?", data.toString());
Bundle extras = data.getExtras();
profilePic = extras.getParcelable("data");
以下是日志:
09-14 11:12:35.271 24647-24647/com.flowonme.chat I/what is data?ï¹ Intent { dat=file:///storage/sdcard0/CometChat Images/IMG20150914111228.jpg typ=image/jpeg }
09-14 11:12:35.274 24647-24647/com.flowonme.chat W/System.errï¹ java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference
09-14 11:12:35.275 24647-24647/com.flowonme.chat W/System.errï¹ at com.inscripts.fragments.SettingsFragment.onActivityResult(SettingsFragment.java:318)
09-14 11:12:35.275 24647-24647/com.flowonme.chat W/System.errï¹ at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:163)
09-14 11:12:35.275 24647-24647/com.flowonme.chat W/System.errï¹ at com.inscripts.utils.SuperActivity.onActivityResult(SuperActivity.java:142)
09-14 11:12:35.275 24647-24647/com.flowonme.chat W/System.errï¹ at android.app.Activity.dispatchActivityResult(Activity.java:6192)
09-14 11:12:35.275 24647-24647/com.flowonme.chat W/System.errï¹ at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
09-14 11:12:35.275 24647-24647/com.flowonme.chat W/System.errï¹ at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
09-14 11:12:35.275 24647-24647/com.flowonme.chat W/System.errï¹ at android.app.ActivityThread.access$1300(ActivityThread.java:151)
09-14 11:12:35.275 24647-24647/com.flowonme.chat W/System.errï¹ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
0