Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Launching Camera Intent from Fragment
    text
    copied!<p>I currently have an activity that hosts multiple fragments and I am on my third fragment of a collection.</p> <p>In that fragment I use an Intent to launch either the Camera or Gallery. See code:</p> <pre><code>public Intent getImageIntent() { // Camera. final List&lt;Intent&gt; cameraIntents = new ArrayList&lt;Intent&gt;(); final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); final PackageManager packageManager = context.getPackageManager(); final List&lt;ResolveInfo&gt; 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; } </code></pre> <p>After that the onActivityResult executes:</p> <pre><code>private void handleSmallCameraPhoto(Intent intent) { Bundle extras = intent.getExtras(); mProductBitmap = (Bitmap) extras.get("data"); imgProduct.setImageBitmap(mProductBitmap); } </code></pre> <p>Where mProductBitmap is a Bitmap Global Variable and imgProduct is an ImageView already initialized.</p> <p><strong>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.</strong></p> <p>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!</p> <p>EDIT: Found out sometimes my Context is Null after the onActivityResult. If anyone has encountered this help is appreciated. Thanks.</p> <p>EDIT: Below is by onActivityResult method, most of the time the first if should be executed.</p> <pre><code>@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(); } } } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload