gpt4 book ai didi

java - 如果从 MyApp 调用文件在其他应用程序中查看,文件不会显示其内容

转载 作者:太空宇宙 更新时间:2023-11-04 11:41:17 25 4
gpt4 key购买 nike


我还有下一个情况。我从 bytesArray 获取文件,从中构建文件,并且不会从其他应用程序查看该文件。

  1. 文件构建正确(我可以从文件系统中看到)
  2. 我可以通过文件系统打开该文件,并且它可以正确打开。
  3. 如果我尝试通过其他应用从我的应用查看此文件,我会遇到一些意外情况。

例如:
我使用 image/jpg,并且不会通过 google photo 查看它,并且应用程序向我显示黑屏,如果我尝试使用某些应用程序操作(图像信息),应用程序就会崩溃。

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”)


我的 AndroidManifest.xml

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


我从 MyApp 看到的内容: From MyApp

我从文件系统中看到的内容:From File System

正在寻求帮助,伙计们。不知道我的 Intent 做错了什么。
谢谢!

更新
如果有人正在寻找工作示例,您需要更改一些代码:

Uri fileUri = getUriForFile(getApplicationContext(), "com.myapp.packadge.fileprovider", outputFile);
intent.setDataAndType(fileUri, mime);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

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>

文件路径.xml

<paths>
<files-path path="internalStorage/outputDir/" name="reports" />
</paths>

最佳答案

在较新版本的 Android 中,它已被更改。现在,您必须在 list 文件中定义提供程序,以便其他应用程序访问您的文件:https://developer.android.com/training/secure-file-sharing/setup-sharing.htmlhttps://developer.android.com/reference/android/support/v4/content/FileProvider.html .

在 stackoverflow 上也得到了解答:How to use support FileProvider for sharing content to other apps?

关于java - 如果从 MyApp 调用文件在其他应用程序中查看,文件不会显示其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42745340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com