How to use support FileProvider for sharing content to other apps?

前端 未结 9 2111
野的像风
野的像风 2020-11-22 14:01

I\'m looking for a way to correctly share (not OPEN) an internal file with external application using Android Support library\'s FileProvider.

Following the example

相关标签:
9条回答
  • 2020-11-22 14:40

    Since as Phil says in his comment on the original question, this is unique and there is no other info on SO on in google, I thought I should also share my results:

    In my app FileProvider worked out of the box to share files using the share intent. There was no special configuration or code necessary, beyond that to setup the FileProvider. In my manifest.xml I placed:

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.my.apps.package.files"
            android:exported="false"
            android:grantUriPermissions="true" >
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/my_paths" />
        </provider>
    

    In my_paths.xml I have:

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

    In my code I have:

        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("application/xml");
    
        Uri uri = FileProvider.getUriForFile(this, "com.my.apps.package.files", fileToShare);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    
        startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share_file)));
    

    And I am able to share my files store in my apps private storage with apps such as Gmail and google drive without any trouble.

    0 讨论(0)
  • 2020-11-22 14:47

    If you get an image from camera none of these solutions work for Android 4.4. In this case it's better to check versions.

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(getContext().getPackageManager()) != null) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            uri = Uri.fromFile(file);
        } else {
            uri = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", file);
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, CAMERA_REQUEST);
    }
    
    0 讨论(0)
  • 2020-11-22 14:51

    just to improve answer given above: if you are getting NullPointerEx:

    you can also use getApplicationContext() without context

                    List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
                    for (ResolveInfo resolveInfo : resInfoList) {
                        String packageName = resolveInfo.activityInfo.packageName;
                        grantUriPermission(packageName, photoURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    }
    
    0 讨论(0)
提交回复
热议问题