![](/img/trans.png)
[英]How do I fix java.io.IOException: write failed: EPIPE (Broken pipe) when I am rooted?
[英]Android FileProvider with photo take intent gives a java.io.IOException: write failed: EPIPE (Broken pipe)
我對新的FileProvider API有問題:
我有以下代碼從文件生成uri:
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;
}
URI是正確的,現在我像這樣打開相機意圖>
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);
}
然后,打開相機,並在拍攝並確認照片后,我具有:
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)
我已經完成了使用FileProvider所需的所有清單更改,並且一周前開始工作。
context.grantUriPermission(context.getPackageName(), result,
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
該代碼顯示為“授予我自己的應用程序許可以使用我自己的應用程序的數據”。 這不是您想要的。 如果要使用grantUriPermission()
,則包名稱必須是您要向其授予許可的應用程序的名稱。
另外,在較新的API級別上還有更簡單的選項:
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);
}
}
我在這:
如果應用程序運行在Android 5.0+上,則只需使用addFlags()
如果應用程序在Android 4.1-4.4上運行,請使用ClipData
在Android 4.0及更低版本上,迭代所有可能的相機應用程序並為它們中的每一個授予權限
在這篇博客文章中 ,我將更多地介紹該代碼段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.