简体   繁体   中英

Get Uri from file I just created (android)

forgive me for what is probably a stupid question, but I really can't find the answer anywhere.

I need my program to save the contents of an EditText as an html file and share it with other applications. To do this, I need to write the file to internal storage and then get the URI to it. However, there is no information anywhere (that I could fine) on how to do that.

This is my code (Editor being the name of the Activity and myapp the package):

String filename = "Lorem ipsum.html";

            FileOutputStream outputStream;

            try {
                outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
                outputStream.write(Html.toHtml(textField.getText()).getBytes());
                outputStream.close();
                System.out.println(filename);
            } catch (Exception e) {
                e.printStackTrace();
            }

                Intent shareIntent = ShareCompat.IntentBuilder.from(Editor.this)
                        .setStream(FileProvider.getUriForFile(Editor.this, "com.myapp.fileprovider", new File(filename)))
                        .getIntent();
                shareIntent.setData(FileProvider.getUriForFile(Editor.this, "com.myapp.fileprovider", new File(filename)));
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(shareIntent);

As far as I can tell, the file creation part works just fine; no exception is caught. However, the actual sharing part crashes with this error:

java.lang.IllegalArgumentException: Failed to find configured root that contains /Lorem ipsum.html

I'm clearly doing something wrong when trying to find the URI, but I've been unable to find a solution. Following the official documentation did not help and I could find no examples of this specific problem.

If it matters, here is my fileprovider declaration:

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

Thank you in advance for any help.

In your code:

FileProvider.getUriForFile(Editor.this, "com.myapp.fileprovider", new File(filename))

The third parameter is equivalent to:

new File("Lorem ipsum.html"");

which is invalid.

You should use the full path of the file as the parameter in the constructor of the File .

As commented by @Fatih you should use

new File(getFilesDir(), filename)

as the third parameter.

Also your authorities parameter in manifest should be corrected as:

android:authorities="${applicationId}.provider" 

So your final statement will be:

Uri uri= FileProvider.getUriForFile(Editor.this,
        getApplicationContext().getPackageName() + ".provider",
        new File(Editor.this.getFilesDir(), filename));

In res/xml folder paths file change:

<?xml version="1.0" encoding="utf-8"?> <paths> <files-path name="files" path="." /> </paths><!-- NOT THIS: <external-path name="external_files" path="."/>-->

In case if authorities parameter in manifest is :

android:authorities="${applicationId}.provider"

And within the java code :

File file = new File(context.getFilesDir(), fileName);
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM