0

I'm programming an app that receives all kind of documents as base64 strings. I've been searching all morning, and I didn't find a way to display the documents properly without storing them. As an alternative I wrote this:

private void createReadableFile(DocumentBinary document) {
    try {
        byte[] bytes = Base64.decode(document.getDocument(), 0);
        FileOutputStream os = openFileOutput(document.getSuggestedFileName(), MODE_PRIVATE);
        os.write(bytes);
        os.flush();
        os.close();
        openFile(document);
    } catch (Exception e) {
        Ln.e(e, "Error while parsing document");
    }
}

And I do this with the created file:

private void openFile(File file, String mimeType) {
    Intent viewIntent = new Intent();
    viewIntent.setAction(Intent.ACTION_VIEW);
    viewIntent.setDataAndType(Uri.fromFile(file), mimeType);
    viewIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(viewIntent);
}

But it doesn't start anything. It calls onPause and onResume but nothing happens. I know that if I change MODE_PRIVATE to MODE_WORLD_READABLE it would work, but MODE_WORLD_READABLE is deprecated. Do you know a better way to do it or what to use instead of MODE_PRIVATE?

1 Answer 1

1
  • Save the file to external storage, or

  • Use FileProvider to create a ContentProvider that serves the file from internal storage, or

  • Create your own ContentProvider that implements openFile() and serves up a stream of content from some other source

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for answering so fast! I've tried to implement it, but I couldn't figure out how to find the package name for each app. I'll be opening images, pdfs, doc, so there is normally an app picker and then the selected app opens.
@Carla: "I couldn't figure out how to find the package name for each app" -- that doesn't really have much to do with this problem. So long as the Intent is properly constructed, particularly with the MIME type, any eligible activity will be considered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.