Andorid calls the camera in Fragment, andoridfragment
I now have three multiple fragments in an activity. I am collecting my third fragments
On this fragment, I want to start the camera or image library and check the following code:
| 12345678910111213141516171819202122232425262728293031323334353637383940 |
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);} |
MProductBitmap is a global variable of the Bitmap type, and imgProduct is an initialized ImageView,
I have the following questions:
1. The camera option forces the app to be disabled, and the nullpointException error is reported in fragment.
2. No error is reported for the Atlas option, but no image is displayed.
3. Sometimes, after onActivityResult is executed, Context is null.
| 1234567891011121314151617181920212223242526272829303132333435 |
@Overridepublic 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(); } } } } } |
Solution
Your image has been reported to the PATH_TO_SAVE address.
Do you need to do it in your onActivityResult method?
File file = new File (PATH_TO_SAVE );
Bitmap bmp = BitmapFactory. decodeFile (file. getPath ());
Address: http://www.itmmd.com/201411/158.html
This article is organized and published by Meng IT personnel. The reprinted article must indicate the source.