i have a problem with new FileProvider API:
I have this code to generate a uri from a file:
public Uri generateUri(String authority) {
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File imageFile = fileHelper.from(storageDir, PHOTO_NAME);
if (!imageFile.exists() && !imageFile.createNewFile()) {
throw new Exception("Can't create capture file");
}
Uri sharedUri = ExtendedFileProvider.getUriForFile(context, authority, imageFile);
return sharedUri;
}
The URI it's correct, and now i open a camera intent like this>
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, result);
context.grantUriPermission(context.getPackageName(), result,
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
context.startActivityForResult(takePictureIntent, BaseActivity.SUBACTIVITY_TAKE_PHOTO);
}
Then, camera it's opened, and when photo is taked and confirmed, i have:
01-10 15:37:27.236 W/FastPrintWriter: Write failure
java.io.IOException: write failed: EPIPE (Broken pipe)
at libcore.io.IoBridge.write(IoBridge.java:501)
at java.io.FileOutputStream.write(FileOutputStream.java:316)
at com.android.internal.util.FastPrintWriter.flushBytesLocked(FastPrintWriter.java:336)
at com.android.internal.util.FastPrintWriter.flushLocked(FastPrintWriter.java:359)
at com.android.internal.util.FastPrintWriter.flush(FastPrintWriter.java:394)
at android.view.ThreadedRenderer.dumpGfxInfo(ThreadedRenderer.java:613)
at android.view.WindowManagerGlobal.dumpGfxInfo(WindowManagerGlobal.java:556)
at android.app.ActivityThread$ApplicationThread.dumpGfxInfo(ActivityThread.java:1175)
at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:577)
at android.os.Binder.execTransact(Binder.java:565)
Caused by: android.system.ErrnoException: write failed: EPIPE (Broken pipe)
at libcore.io.Posix.writeBytes(Native Method)
at libcore.io.Posix.write(Posix.java:273)
at libcore.io.BlockGuardOs.write(BlockGuardOs.java:319)
at libcore.io.IoBridge.write(IoBridge.java:496)
at java.io.FileOutputStream.write(FileOutputStream.java:316)
at com.android.internal.util.FastPrintWriter.flushBytesLocked(FastPrintWriter.java:336)
at com.android.internal.util.FastPrintWriter.flushLocked(FastPrintWriter.java:359)
at com.android.internal.util.FastPrintWriter.flush(FastPrintWriter.java:394)
at android.view.ThreadedRenderer.dumpGfxInfo(ThreadedRenderer.java:613)
at android.view.WindowManagerGlobal.dumpGfxInfo(WindowManagerGlobal.java:556)
at android.app.ActivityThread$ApplicationThread.dumpGfxInfo(ActivityThread.java:1175)
at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:577)
at android.os.Binder.execTransact(Binder.java:565)
I have done all manifest changes required to use FileProvider and it was working a week ago.. Any idea?
context.grantUriPermission(context.getPackageName(), result,
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
This code says "grant my own app permission to use my own app's data". This is not what you want. If you want to use grantUriPermission()
, the package name needs to be for the app to whom you are granting permission.
Plus, there are simpler options on newer API levels:
i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
i.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", outputUri);
i.setClipData(clip);
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else {
List<ResolveInfo> resInfoList=
getPackageManager()
.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, outputUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}
Here, I:
Just use addFlags()
, if the app is running on Android 5.0+
Use ClipData
, if the app is running on Android 4.1-4.4
Iterate over all possible camera apps and grant the permission to each of them, on Android 4.0 and older
I cover that code snippet more in this blog post .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.