How to share a file in Android programmatically

前端 未结 4 1209
旧时难觅i
旧时难觅i 2020-12-28 19:28

I want to share a file(.pdf,.apk etc) using share Intent, I searched google but I find only the code for sharing Image

Intent sharingIntent = new Intent(Inte         


        
相关标签:
4条回答
  • 2020-12-28 20:14

    This will help you

    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    Uri screenshotUri = Uri.parse(path);
    sharingIntent.setType("*/*");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
    startActivity(Intent.createChooser(sharingIntent, "Share using"));
    
    0 讨论(0)
  • 2020-12-28 20:26

    You use the same code, you just change the MIME type for the type of data you wish to share. If you wish to share anything regardless of type, use */*

    0 讨论(0)
  • 2020-12-28 20:26

    For sdk 24 and up, if you need to get the Uri of a file outside your app storage you have this error.

    android.os.FileUriExposedException: file:///storage/emulated/0/MyApp/Camera_20180105_172234.jpg exposed beyond app through ClipData.Item.getUri()
    

    to fix this : exposed beyond app through ClipData.Item.getUri

    0 讨论(0)
  • 2020-12-28 20:30

    To keep your code pragmatic, use ShareCompat class:

    ShareCompat.IntentBuilder.from(this)
            .setStream(uri)
            .setType(URLConnection.guessContentTypeFromName(file.getName()))
            .startChooser();
    
    0 讨论(0)
提交回复
热议问题