在 android 中使用 getParcelable 检索位图文件时出现错误

I am getting an error while retrieving the Bitmap file using getParcelable in android

我正在尝试获取我已传递到意图中的位图文件,但每次 getParcelable() 方法 return 都为空。

以下是我的 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");

以下是我的 onActivityResult() 方法,来自 SettingFragment.java

@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.access00(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

如这里:

Intent { dat=file:///storage/sdcard0/CometChat Images/IMG20150914111228.jpg typ=image/jpeg }

正在获取图像的 URI 作为响应。所以从 URI 获取位图为:

Uri imageUri = data.getData();
profilePic = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);

我遇到过关于一些 Android 5.0 phone 的问题。我认为这是关于 URI。 你可以试试这个。

Bitmap avatar = null;
if (data.hasExtra("data"))
{
    avatar = data.getParcelableExtra("data");
}
else
{
    BitmapFactory.Options options = new BitmapFactory.Options();
    InputStream is = null;
    try
    {
            is = getContentResolver().openInputStream(data.getData());
            avatar = BitmapFactory.decodeStream(is, null, options);
    }
    catch (Exception e)
    {
        LogUtils.error("file not found");
    }
    finally
    {
        try
        {
            is.close();
        }
        catch (Exception e)
        {
        }
    }
}

我建议你使用URIBitmap 可能 OOM。