简体   繁体   English

具有拍摄意图的Android FileProvider给出java.io.IOException:写入失败:EPIPE(管道损坏)

[英]Android FileProvider with photo take intent gives a java.io.IOException: write failed: EPIPE (Broken pipe)

i have a problem with new FileProvider API: 我对新的FileProvider API有问题:

I have this code to generate a uri from a file: 我有以下代码从文件生成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;
    }

The URI it's correct, and now i open a camera intent like this> 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);
            }

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? 我已经完成了使用FileProvider所需的所有清单更改,并且一周前开始工作。

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. 如果要使用grantUriPermission() ,则包名称必须是您要向其授予许可的应用程序的名称。

Plus, there are simpler options on newer API levels: 另外,在较新的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);
  }
}

Here, I: 我在这:

  • Just use addFlags() , if the app is running on Android 5.0+ 如果应用程序运行在Android 5.0+上,则只需使用addFlags()

  • Use ClipData , if the app is running on Android 4.1-4.4 如果应用程序在Android 4.1-4.4上运行,请使用ClipData

  • Iterate over all possible camera apps and grant the permission to each of them, on Android 4.0 and older 在Android 4.0及更低版本上,迭代所有可能的相机应用程序并为它们中的每一个授予权限

I cover that code snippet more in this blog post . 这篇博客文章中 ,我将更多地介绍该代码段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 java.io.IOException: write failed: EPIPE (Broken pipe) 当我扎根时? - How do I fix java.io.IOException: write failed: EPIPE (Broken pipe) when I am rooted? java.io.IOException: 管道损坏 - java.io.IOException: Broken pipe Android错误:写入失败:EPIPE(管道断开) - Android error: write failed: EPIPE (Broken pipe) PipedInputStream - 如何避免“java.io.IOException:Pipe broken” - PipedInputStream - How to avoid “java.io.IOException: Pipe broken” 避免日志充满 java.io.IOException: Broken pipe - Avoid logs full of java.io.IOException: Broken pipe java.net.SocketException:sendto失败:Android上的EPIPE(管道断开) - java.net.SocketException: sendto failed: EPIPE (Broken pipe) on Android 提交响应 java.io.IOException 时出错:sun.nio.ch.FileDispatcher.write0(Native Method) 处的管道损坏 - Error commiting response java.io.IOException: Broken pipe at sun.nio.ch.FileDispatcher.write0(Native Method) java.io.IOException:无法编写 react-native run-android 失败 - java.io.IOException: Can't write react-native run-android failed Android java.io.IOException:写入失败:EBADF(错误的文件号) - Android java.io.IOException: write failed: EBADF (Bad file number) 索引器:java.io.IOException:作业失败 - Indexer: java.io.IOException: Job failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM