0

I am trying to get my Bitmap file which I have passed into an intent but getParcelable() method return null everytime.

following is my java code where I am sending my Bitmap image in intent:

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);

following is my java code where I am trying to retrieving the image :

Bitmap profilePic;
String fileName, filePath;
Log.i("what is data?", data.toString());
Bundle extras = data.getExtras();
profilePic = extras.getParcelable("data");

Getting an issue on last line of above code which is:

profilePic = extras.getParcelable("data");

Following are my onActivityResult() methode from 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");

Following are the logs:

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

2 Answers2

1

As here :

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

Getting URI of image in response. so get Bitmap from URI as:

Uri imageUri = data.getData();
profilePic = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • getting an issue on line profilePic = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri); Can not resolve method getContentResolver –  Sep 14 '15 at 07:11
  • @Albert: use Activity Context to access this method like `Your_Activity_Name.this.getContentResolver()` – ρяσѕρєя K Sep 14 '15 at 07:14
  • I have tried following code but still same issue. `Uri imageUri = data.getData(); profilePic = MediaStore.Images.Media.getBitmap(SettingsFragment.this.getContentResolver(),imageUri);` I have also confirm my activity using following code in onCreate `Log.i("WhichActivity", this.getClass().getSimpleName());` –  Sep 14 '15 at 07:27
  • @Albert: Use `SettingsFragment.this.getActivity().getContentResolver(), ` – ρяσѕρєя K Sep 14 '15 at 07:29
  • @Albert: Welcome . if it help then why unaccepted ? – ρяσѕρєя K Sep 14 '15 at 09:13
0

I have encounter this questions about some Android 5.0 phone. I think it is about the URI. You can try this.

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)
        {
        }
    }
}

I suggest you use URI. Bitmap maybe OOM.

Egos Zhang
  • 1,334
  • 10
  • 18