1

I have an android apk expansion file and in there are some PDF's. In the official documentation they access the files inside the .obb via Inputstream. I am able to access the files inside the .obb via the inputstream.

Now I want to attach one of the files to an email with Intent. The E-Mail Intent works perfectly fine with files from the assets, so the problem is attaching the Inputstream.

How can I attach the PDF into the mail directly from the .obb?

  • Possible duplicate of [How to send an email with a file attachment in Android](http://stackoverflow.com/questions/9974987/how-to-send-an-email-with-a-file-attachment-in-android) – StarWind0 Jan 03 '17 at 22:18

1 Answers1

0

Solved it!

You have to convert the Inputstream to a Temorary File, get the Uri of that file and attach it to the email Intent.

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("application/pdf");

    try {
        ZipResourceFile expansionFile = new ZipResourceFile("Path to .obb file");
        InputStream fileStream = expansionFile.getInputStream("Path inside .obb");

        String downloadordner = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(); //used for temp storage

        File tempFile = new File(downloadordner+"/"+"filename.pdf");
        tempFile.deleteOnExit();
        FileOutputStream out = new FileOutputStream(tempFile);
        IOUtils.copy(fileStream, out);

        Uri theUri = Uri.fromFile(tempFile);

        i.putExtra(Intent.EXTRA_STREAM, theUri);
        startActivity(Intent.createChooser(i, "PDF versenden..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(preisliste.this, "Es wurde kein E-Mail Client gefunden.", Toast.LENGTH_SHORT).show();
    }
    catch (IOException e)
    {
        Log.v("Datei nicht gefunden","Main Expansion");
    }