How we are view and share a file from the application?
- First of All get the permission of Storage of device.
1 |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> |
add in our manifest file.
- Find the path of your file-
1 2 3 4 5 6 7 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { pdfFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); } else{ pdfFolder = Environment.getExternalStorageDirectory(); } fileName = "Invoice_detail" + ".pdf"; myFile = new File(pdfFolder.getAbsolutePath() + File.separator + fileName); |
- For View, this file startActivity with the following Intent from our application.
1 2 3 4 5 6 7 8 9 10 11 |
public void viewPdf() { if(myFile.exist()){ Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(myFile), "application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent); }else{ // method is showing toast showToast("File does not exist") } } |
- For Share, this file startActivity with the following Intent from our application.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public void shareFile(){ Intent intentShareFile = new Intent(Intent.ACTION_SEND); if(myFile.exists()) { intentShareFile.setType("application/pdf"); intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myFile)); intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "Sharing File from Webkul..."); intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File from Webkul to purchase items..."); context.startActivity(Intent.createChooser(intentShareFile, "Share File Webkul")); } } |