Fragment
do with below Button Click code
After Intent pass, we can Create Bitmap from Uri Code within onActivityResult
// Now this Bitmap you can set as per need in Imageview imagePhoto
do with below Button Click code
mCamerabtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); if (checkPermission(wantCameraPermission)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File file = new File(Environment.getExternalStorageDirectory(),"test.jpg"); outputFileUri = Uri.fromFile(file); Log.d("TAG", "outputFileUri intent" + outputFileUri); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivityForResult(intent, 111); } else { requestPermission(wantCameraPermission, PERMISSION_REQUEST_CODE_Camera); } dialog.cancel(); } });
Check for require permission above 6.0
private boolean checkPermission(String permission) {
if (Build.VERSION.SDK_INT >= 23) { int result = ContextCompat.checkSelfPermission(mcontext, permission); if (result == PackageManager.PERMISSION_GRANTED) { return true; } else { return false; } } else { return true; } }
private void requestPermission(String permission, int permission_requestcode) { requestPermissions(new String[]{permission}, permission_requestcode); }
@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grants) { List<Fragment> fragments = getChildFragmentManager().getFragments(); //==================Start switch (requestCode) { case PERMISSION_REQUEST_CODE_Camera: Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File file = new File(Environment.getExternalStorageDirectory(), "test.jpg"); outputFileUri = Uri.fromFile(file); Log.d("TAG", "outputFileUri intent" + outputFileUri); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); getActivity().startActivityForResult(intent, 111); break; case PERMISSION_REQUEST_CODE: if (grants.length > 0 && grants[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(mcontext, "Permission Granted. Now you can Read data.", Toast.LENGTH_LONG).show(); ImageLoad(); } else { Toast.makeText(mcontext, "Permission Denied. You cannot Read data.", Toast.LENGTH_LONG).show(); } break; } //================End if (fragments != null) { for (Fragment fragment : fragments) { if (fragment != null) { fragment.onRequestPermissionsResult(requestCode, permissions, grants); } } } }
After Intent pass, we can Create Bitmap from Uri Code within onActivityResult
@Overridepublic void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); switch (reqCode) { case 111:
Bitmap bitmap = decodeSampledBitmapFromUri(outputFileUri, 300, 400);if (resultCode == RESULT_OK) { Log.d("TAG", "outputFileUri RESULT_OK" + outputFileUri); if (outputFileUri != null) {
// Now this Bitmap you can set as per need in Imageview imagePhoto
imagePhoto.setImageBitmap(bitmap);
Here in below function we need to pass Uri, int reqWidth, int reqHeightbreak; }
}
public Bitmap decodeSampledBitmapFromUri(Uri uri, int reqWidth, int reqHeight) { Bitmap bm = null; try { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(((AppCompatActivity) mcontext).getContentResolver().openInputStream(uri), null, options); // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false; bm = BitmapFactory.decodeStream(((AppCompatActivity) mcontext).getContentResolver().openInputStream(uri), null, options); } catch (FileNotFoundException e) { e.printStackTrace(); Toast.makeText(mcontext, e.toString(), Toast.LENGTH_LONG).show(); } return bm; }
No comments:
Post a Comment
Comments