如何查看通过内容内容解析器保存的文件。

huangapple go评论78阅读模式
英文:

how to view file saved via content Content Resolver

问题

I have saved a PDF file to the documents directory using contentResolver. it is getting saved successfully. However, I'm not able to open the file via code.

以下是代码部分:

public void saveToExternalStorage(File file, Context context) {
    ContentValues cv = new ContentValues();
    cv.put(MediaStore.Files.FileColumns.DISPLAY_NAME, "order" + new Date().getTime() + ".pdf");
    cv.put(MediaStore.Files.FileColumns.MIME_TYPE, "application/pdf");
    cv.put(MediaStore.Files.FileColumns.RELATIVE_PATH, "Documents");
    cv.put(MediaStore.Files.FileColumns.IS_PENDING, 1);

    ContentResolver resolver = context.getContentResolver();
    Uri uri = resolver.insert(MediaStore.Files.getContentUri("external"), cv);
    if (uri != null) {

        try {
            OutputStream outputStream = resolver.openOutputStream(uri);
            byte[] bytes = new byte[(int) file.length()];
            FileInputStream fis = new FileInputStream(file);
            fis.read(bytes);
            outputStream.write(bytes);
            outputStream.close();
            Log.e("file", "saved" + uri);
            cv.put(MediaStore.Downloads.IS_PENDING, 0);
            resolver.update(uri, cv, null, null);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(intent);

        } catch (Exception e) {
            Log.e("error", Log.getStackTraceString(e.fillInStackTrace()));
        }
    }
}

模拟器打开一个带有空白屏幕的应用程序。

英文:

I have saved a PDF file to the documents directory using contentResolver. it is getting saved successfully. However, I'm not able to open the file via code.

following is the code

public void saveToExternalStorage(File file, Context context) {
ContentValues cv = new ContentValues();
cv.put(MediaStore.Files.FileColumns.DISPLAY_NAME, "order" + new Date().getTime() + ".pdf");
cv.put(MediaStore.Files.FileColumns.MIME_TYPE, "application/pdf");
cv.put(MediaStore.Files.FileColumns.RELATIVE_PATH, "Documents");
cv.put(MediaStore.Files.FileColumns.IS_PENDING, 1);
ContentResolver resolver = context.getContentResolver();
Uri uri = resolver.insert(MediaStore.Files.getContentUri("external"), cv);
if (uri != null) {
try {
OutputStream outputStream = resolver.openOutputStream(uri);
byte[] bytes = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bytes);
outputStream.write(bytes);
outputStream.close();
Log.e("file", "saved" + uri);
cv.put(MediaStore.Downloads.IS_PENDING, 0);
resolver.update(uri, cv, null, null);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
} catch (Exception e) {
Log.e("error", Log.getStackTraceString(e.fillInStackTrace()));
}
}
}

the emulator opens an app with a blank screen.
如何查看通过内容内容解析器保存的文件。

答案1

得分: 1

唯一需要添加的是在意图中添加一个读取权限标志。

英文:

The only thing you need to add is a READ permission flag to the intent.

huangapple
  • 本文由 发表于 2023年5月24日 18:54:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定