Android app directory structure, email files from internal storage

余生长醉 提交于 2019-12-25 04:24:29

问题


I have a problem. I'm currently saving pdf files in the internal storage /data/user/0/com.thatapp.myApp/files/JP_31072016065930.pdf

Reading it via the app is not a problem, so I'm positive it exists. I'm now trying to send the file via email. From the other questions and answers here, I gather that you need to use a file provider.

So I added the following to my manifest

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

@xml/filepath contains

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_pdf" path=""/>

My send email function is as below

String[] TO = {""};
    String[] CC = {""};
    Intent emailIntent = new Intent(Intent.ACTION_SEND);

    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
    emailIntent.putExtra(Intent.EXTRA_CC, CC);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "OTP:" + fileNmStr);
    emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
    File file = new File(fileNmStr);
    Log.i("PDF FILE", fileNmStr);
    Uri contentUri = getUriForFile(this, "com.thatapp.fileprovider", file);
    emailIntent.putExtra(Intent.EXTRA_STREAM, contentUri);

    try {
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        finish();
        Toast.makeText(this, "Email Sent.", Toast.LENGTH_SHORT).show();
    }
    catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
    }

The File logs as /data/user/0/com.thatapp.contract/files/OTP_JP_31072016065930.pdf

When I run the code I hit the exception

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.thatapp.myApp/files/OTP_JP_31072016065930.pdf

So if from what I understand /data/user/0/ == /data/data/ , then what causes the problem? Is my filepath wrong? I also tried "files/" as the path in the filepath but have the same problem. Please help.... staring at the problem for 24hrs already

来源:https://stackoverflow.com/questions/38682204/android-app-directory-structure-email-files-from-internal-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!