Java 如果文件从MyApp调用以在其他应用中查看,则不会显示其内容

Java 如果文件从MyApp调用以在其他应用中查看,则不会显示其内容,java,android,android-intent,Java,Android,Android Intent,我有下一个情况。 我从bytesArray获取文件,从中构建文件,然后从其他应用程序中查看该文件 文件构建正确(我可以从文件系统中看到) 我可以通过文件系统打开该文件,并且它可以正确打开 如果我试图通过其他应用程序从我的应用程序查看此文件,我会遇到一些意外情况 例如: 我使用image/jpg,wonna通过谷歌照片查看,应用程序会显示黑屏,如果我尝试使用一些应用程序操作(图像信息),应用程序就会崩溃 File file, outputFile = null; String fileName;


我有下一个情况。 我从bytesArray获取文件,从中构建文件,然后从其他应用程序中查看该文件

  • 文件构建正确(我可以从文件系统中看到)
  • 我可以通过文件系统打开该文件,并且它可以正确打开
  • 如果我试图通过其他应用程序从我的应用程序查看此文件,我会遇到一些意外情况
  • 例如:
    我使用image/jpg,wonna通过谷歌照片查看,应用程序会显示黑屏,如果我尝试使用一些应用程序操作(图像信息),应用程序就会崩溃

    File file, outputFile = null;
    String fileName;
    if(android.os.Environment.getExternalStorageState()
       .equals(android.os.Environment.MEDIA_MOUNTED)) {
         file = new File(Environment.getExternalStorageDirectory(), ".MyApp/Files/");
         if (!file.exists()) {
            file.mkdirs();
         }
         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
         fileName = "report_" + sdf.format(new Date());
         outputFile = new File(file.getAbsolutePath() + "/" + fileName + finalExtention);
    }
    FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
    fileOutputStream.write(bytes);
    fileOutputStream.close();
    Intent intent = new Intent();
    String mime = getMimeType(outputFile.getAbsolutePath());
    String filePath = outputFile.getAbsolutePath();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse(filePath), mime);
    Intent intentChooser = Intent.createChooser(intent, "Choose an application to open with:");
    startActivity(intentChooser);
    

    getMimeType返回字符串,如“image/jpeg”
    finalExtension来自json(对于本例为“.jpg”)


    My AndroidManifest.xml

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
    AndroidManifest.xml

     <application
          ....
    
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.myApp.packadge.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
    

    在较新版本的Android中,它已被更改。现在,您必须在清单文件中定义提供者,以允许其他应用程序选项访问您的文件:和


    stackoverflow上也有回答:

    在较新版本的Android中,它已被更改。现在,您必须在清单文件中定义提供者,以允许其他应用程序选项访问您的文件:和


    stackoverflow上也有回答:

    您确定写入文件的
    字节是正确的jpg格式吗?您确定写入文件的
    字节是正确的jpg格式吗?谢谢,这是我的工作。已经应用了一些更改及其工作!谢谢,这是我的工作。已经应用了一些更改及其工作!
    
     <application
          ....
    
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.myApp.packadge.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
    
    <paths>
        <files-path path="internalStorage/outputDir/" name="reports" />
    </paths>