1
votes

I currently have an activity that hosts multiple fragments and I am on my third fragment of a collection.

In that fragment I use an Intent to launch either the Camera or Gallery. See code:

public Intent getImageIntent() {

    // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = context.getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName,
                res.activityInfo.name));
        intent.setPackage(packageName);
        cameraIntents.add(intent);
    }

    // Filesystem.
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
            cameraIntents.toArray(new Parcelable[] {}));

    // Calling activity should exeecute:
    // startActivityForResult(chooserIntent, 1);
    return chooserIntent;
}

After that the onActivityResult executes:

private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mProductBitmap = (Bitmap) extras.get("data");
    imgProduct.setImageBitmap(mProductBitmap);
}

Where mProductBitmap is a Bitmap Global Variable and imgProduct is an ImageView already initialized.

For some reason, first: The Camera option force closes the app, and gets a null pointer from the actual fragment itself, like the fragment items are all null again. Second: The gallery options (More then one) don't error out but don't show the image either.

Any help would be appreciated. I've looked at every response possible, but not many people call an intent from a fragment that isn't the initial fragment that the activity is hosting. Thanks!

EDIT: Found out sometimes my Context is Null after the onActivityResult. If anyone has encountered this help is appreciated. Thanks.

EDIT: Below is by onActivityResult method, most of the time the first if should be executed.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (resultCode == Activity.RESULT_OK) {
        handleSmallCameraPhoto(intent);
    } else {
        if (requestCode == 1) {
            InputStream stream = null;
            if (intent == null) {
                System.out.println("DATA IS NULL..");
            } else {
                try {
                    if (mProductBitmap != null) {
                        mProductBitmap.recycle();
                    }
                    stream = getActivity().getContentResolver().openInputStream(
                            intent.getData());
                    mProductBitmap = BitmapFactory.decodeStream(stream);
                    System.out.println(mProductBitmap);
                    System.out.println("Setting image result");
                    imgProduct.setImageBitmap(mProductBitmap);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    if (stream != null)
                        try {
                            stream.close();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                        }
                }
            }
        }
    }

}
1
where you are initializing context in Fragment? - Gopal Gopi
Can you show the code where you do startActivityForResult(...) ? - yurezcv
@GopalRao The context in a fragment is taken from its activity, so in this instance I use a CameraImage object and initialize the context when constructing it. - John Shelley
@yurezcv I edited my initial question with the method. Hope that helps, but not much more needed. - John Shelley
Did you solve that problem? I am having same issue - Gokhan Arik

1 Answers

0
votes

Your photo is saved in PATH_TO_SAVE location.

You should in onActivityResult do something like this:

File file = new File(PATH_TO_SAVE);
Bitmap bmp = BitmapFactory.decodeFile(file.getPath());