In android manifest added the FileProvider inside the application tag
<application
.
.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
This is the xml provider_paths
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="files" path="."/>
</paths>
This how starting the IMAGE CAPTURE intent:
public void imageCapture() {
try{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_" + ".jpeg";
//Here file is created with the imageFileName
photoFile = FileClientService.getFilePath(imageFileName, getApplicationContext(), "image/jpeg");
//Content URI for file
capturedImageUri = FileProvider.getUriForFile(this,getApplicationContext().getPackageName()+".provider",photoFile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
cameraIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
ClipData clip=
ClipData.newUri(getContentResolver(), "a Photo", capturedImageUri);
cameraIntent.setClipData(clip);
cameraIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
List<ResolveInfo> resInfoList=
getPackageManager()
.queryIntentActivities(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, capturedImageUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}
if (cameraIntent.resolveActivity(getApplicationContext().getPackageManager()) != null) {
if (photoFile != null) {
startActivityForResult(cameraIntent, MultimediaOptionFragment.REQUEST_CODE_TAKE_PHOTO);
}
}
}catch (Exception e){
e.printStackTrace();
}
}
This is the file created for writing into this file
/storage/emulated/0/xyz/image/JPEG_20170110_141532_.jpeg
The Content Uri which i'm getting in onActivityResult :
content://com.example.sample.provider/files/xyz/image/JPEG_20170110_141532_.jpeg
This is the onAcivityResult method
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
try {
if ((requestCode == REQUEST_CODE_ATTACH_PHOTO ||
requestCode == REQUEST_CODE_TAKE_PHOTO)
&& resultCode == Activity.RESULT_OK) {
Uri selectedFileUri = (intent == null ? null : intent.getData());
File file = null;
if (selectedFileUri == null) {
selectedFileUri = ((ConversationActivity) fragmentActivity).getCapturedImageUri();
file = ((ConversationActivity) fragmentActivity).getFileObject();
}
if (selectedFileUri == null) {
Bitmap photo = (Bitmap) (intent != null ? intent.getExtras().get("data") : null);
selectedFileUri = ImageUtils.getImageUri(fragmentActivity, photo);
}
if (selectedFileUri != null) {
file = ((ConversationActivity) fragmentActivity).getFileObject();
}
getConversationFragment().loadFile(selectedFileUri, file);
Log.i(TAG, "File uri: " + selectedFileUri);
}
} catch (Exception e) {
}
}
This is the method I'm using for opening content URI, I have both content URI and file to write
public void writeFile(Uri uri,File file) {
InputStream in = null;
OutputStream out = null;
try {
in = context.getContentResolver().openInputStream(uri);//I have context object
byte[] buffer = new byte[1024];
int bytesRead = -1;
out = new FileOutputStream(file);
while ((bytesRead = in.read(buffer)) != -1) { // here in.read(buffer)) is is always -1
out.write(buffer, 0, bytesRead);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (in != null && out != null) {
try {
out.flush();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
I'm facing the problem in opening the content URI for writing into the file created the input stream always returning -1 in above writeFile method when I checked in logs the in.read(buffer)) returning -1
How to solve this problem. Any help is appreciated