try this code :
if you are testing in N+ SDk add this things
create xml/provider_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
AndroidManifest.xml: we specify our provider. Make sure to set exported to false and grantUriPermissions to true! If you want to create a custom FileProvider (a class which extends FileProvider), then you need to change the name of the provider.
<application>
...
<!-- Needed for Android >= Nougat for file access -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mypackage.myprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
...
</application>
for more visit this link: https://medium.com/@ali.dev/open-a-file-in-another-app-with-android-fileprovider-for-android-7-42c9abb198c1
try below solution. work for me tested.
private void pickImage() {
try {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
getActivity().startActivityForResult(Intent.createChooser(intent, "Pick Image"), 101);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private void captureImage() {
try {
if (getActivity().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageFilePath = getPathUri(getActivity());
assert imageFilePath != null;
manager.setLogoPath(imageFilePath.toString());
if (imageFilePath != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFilePath);
getActivity().startActivityForResult(intent, 102);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == 101) {
if (resultCode == RESULT_OK) {
try {
Uri resultUri = data.getData();
String path = getUriRealPath(getApplicationContext(), resultUri);
File imgFile = new File(path);
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
myBitmap = CommonMethods.scaleCenterCrop(myBitmap, 150, 150);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
application.getObserver().setLogoPath(encoded);
}
} catch (Exception ex) {
ex.printStackTrace();
DebugLog.e("ERROR => " + ex.getMessage());
}
}
} else if (requestCode == 102) {
if (resultCode == RESULT_OK) {
if (data != null && data.getExtras() !=null){
Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
imageViewLogo.setImageBitmap(imageBitmap);
} else {
Uri uri = Uri.parse(manager.getLogoPath());
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
bitmap = CommonMethods.scaleCenterCrop(bitmap, 150, 150);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
application.getObserver().setLogoPath(encoded);
imageViewLogo.setImageBitmap(bitmap);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getUriRealPath(Context ctx, Uri uri) {
String ret = "";
if (isAboveKitKat()) {
// Android OS above sdk version 19.
ret = getUriRealPathAboveKitkat(ctx, uri);
} else {
// Android OS below sdk version 19
ret = getImageRealPath(ctx.getContentResolver(), uri, null);
}
return ret;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private static String getUriRealPathAboveKitkat(Context ctx, Uri uri) {
String ret = "";
if (ctx != null && uri != null) {
if (isContentUri(uri)) {
if (isGooglePhotoDoc(uri.getAuthority())) {
ret = uri.getLastPathSegment();
} else {
ret = getImageRealPath(ctx.getContentResolver(), uri, null);
}
} else if (isFileUri(uri)) {
ret = uri.getPath();
} else if (isDocumentUri(ctx, uri)) {
// Get uri related document id.
String documentId = DocumentsContract.getDocumentId(uri);
// Get uri authority.
String uriAuthority = uri.getAuthority();
if (isMediaDoc(uriAuthority)) {
String idArr[] = documentId.split(":");
if (idArr.length == 2) {
// First item is document type.
String docType = idArr[0];
// Second item is document real id.
String realDocId = idArr[1];
// Get content uri by document type.
Uri mediaContentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if ("image".equals(docType)) {
mediaContentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(docType)) {
mediaContentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(docType)) {
mediaContentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
// Get where clause with real document id.
String whereClause = MediaStore.Images.Media._ID + " = " + realDocId;
ret = getImageRealPath(ctx.getContentResolver(), mediaContentUri, whereClause);
}
} else if (isDownloadDoc(uriAuthority)) {
// Build download uri.
Uri downloadUri = Uri.parse("content://downloads/public_downloads");
// Append download document id at uri end.
Uri downloadUriAppendId = ContentUris.withAppendedId(downloadUri, Long.valueOf(documentId));
ret = getImageRealPath(ctx.getContentResolver(), downloadUriAppendId, null);
} else if (isExternalStoreDoc(uriAuthority)) {
String idArr[] = documentId.split(":");
if (idArr.length == 2) {
String type = idArr[0];
String realDocId = idArr[1];
if ("primary".equalsIgnoreCase(type)) {
ret = Environment.getExternalStorageDirectory() + "/" + realDocId;
}
}
}
}
}
return ret;
}
/* Check whether current android os version is bigger than kitkat or not. */
private static boolean isAboveKitKat() {
boolean ret = false;
ret = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
return ret;
}
/* Check whether this uri represent a document or not. */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private static boolean isDocumentUri(Context ctx, Uri uri) {
boolean ret = false;
boolean ver = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (ctx != null && uri != null) {
if (ver) {
ret = DocumentsContract.isDocumentUri(ctx, uri);
}
}
return ret;
}
/* Check whether this uri is a content uri or not.
* content uri like content://media/external/images/media/1302716
* */
private static boolean isContentUri(Uri uri) {
boolean ret = false;
if (uri != null) {
String uriSchema = uri.getScheme();
if ("content".equalsIgnoreCase(uriSchema)) {
ret = true;
}
}
return ret;
}
/* Check whether this uri is a file uri or not.
* file uri like file:///storage/41B7-12F1/DCIM/Camera/IMG_20180211_095139.jpg
* */
private static boolean isFileUri(Uri uri) {
boolean ret = false;
if (uri != null) {
String uriSchema = uri.getScheme();
if ("file".equalsIgnoreCase(uriSchema)) {
ret = true;
}
}
return ret;
}
/* Check whether this document is provided by ExternalStorageProvider. */
private static boolean isExternalStoreDoc(String uriAuthority) {
boolean ret = false;
if ("com.android.externalstorage.documents".equals(uriAuthority)) {
ret = true;
}
return ret;
}
/* Check whether this document is provided by DownloadsProvider. */
private static boolean isDownloadDoc(String uriAuthority) {
boolean ret = false;
if ("com.android.providers.downloads.documents".equals(uriAuthority)) {
ret = true;
}
return ret;
}
/* Check whether this document is provided by MediaProvider. */
private static boolean isMediaDoc(String uriAuthority) {
boolean ret = false;
if ("com.android.providers.media.documents".equals(uriAuthority)) {
ret = true;
}
return ret;
}
/* Check whether this document is provided by google photos. */
private static boolean isGooglePhotoDoc(String uriAuthority) {
boolean ret = false;
if ("com.google.android.apps.photos.content".equals(uriAuthority)) {
ret = true;
}
return ret;
}
/* Return uri represented document file real local path.*/
private static String getImageRealPath(ContentResolver contentResolver, Uri uri, String whereClause) {
String ret = "";
// Query the uri with condition.
Cursor cursor = contentResolver.query(uri, null, whereClause, null, null);
if (cursor != null) {
boolean moveToFirst = cursor.moveToFirst();
if (moveToFirst) {
// Get columns name by uri type.
String columnName = MediaStore.Images.Media.DATA;
if (uri == MediaStore.Images.Media.EXTERNAL_CONTENT_URI) {
columnName = MediaStore.Images.Media.DATA;
} else if (uri == MediaStore.Audio.Media.EXTERNAL_CONTENT_URI) {
columnName = MediaStore.Audio.Media.DATA;
} else if (uri == MediaStore.Video.Media.EXTERNAL_CONTENT_URI) {
columnName = MediaStore.Video.Media.DATA;
}
// Get column index.
int imageColumnIndex = cursor.getColumnIndex(columnName);
// Get column value which is the uri related file local path.
ret = cursor.getString(imageColumnIndex);
}
}
return ret;
}
public static Uri getPathUri(Context context) {
try {
String strDate = (new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US)).format(new Date());
File fileDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "APP");
if (!fileDirectory.exists()) {
if (!fileDirectory.mkdirs()) {
DebugLog.e("Oops! Failed create : \n" + "APP" + " directory");
}
}
File f = new File(fileDirectory.getPath() + File.separator, "IMG_" + strDate + ".jpeg");
if (Build.VERSION.SDK_INT >= 24) {
return FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", f);
} else {
return Uri.fromFile(f);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}