I know how to share file from SD Card, but this time i have to share file from assets folder.
See below code, which i used to share file from SD
String fileToShare = Environment.getExternalStorageDirectory().getPath() +"/Downloads/" + fileName;
newFile = new File(fileToPrint);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("audio/mp3");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(newFile));
startActivity(intent);
Related
how to get pdf from internal storage in android Using WebView or AnyConcept. i have to get pdf in an activity from internal storage.Can anyone of u guys let me know about this .Thank You.
String filename = null;
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
startActivity(intent);
and give pdf filename
I tried many ways but I can't do this.
I have a *.txt file. I want to share it via Bluetooth, wifi, email and ....
When i used this code i cant share the file:
File file = new File(Environment.getExternalStorageDirectory(), "Email-Ghap/Emails.txt");
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("*/txt");
sharingIntent.putExtra(Intent.EXTRA_STREAM, file);
startActivity(Intent.createChooser(sharingIntent, "share file with"));
Totally I want this: when the user clicked on the share button and chooses one email sender like Gmail for sharing. the file must be attached file for the new email ...
I found this link https://stackoverflow.com/a/16036249/4016922
But it shares txt file content. I want to share the file not the content of the txt file
Change your Code Like this.
From
File file = new File(Environment.getExternalStorageDirectory(), "Email-Ghap/Emails.txt");
To
File file = new File(Environment.getExternalStorageDirectory() + "/" + "Email-Ghap/Emails.txt");
from:
sharingIntent.setType("*/txt");
To
sharingIntent.setType("text/*");
so yourFinal Code Looks Like
File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + "abc.txt");
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getAbsolutePath()));
startActivity(Intent.createChooser(sharingIntent, "share file with"));
My app use MP3 files with MediaPlayer, i want to make a button than will share an MP3 file to whatsapp.
my code is this:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
String audioClipFileName="bell.mp3";
sendIntent.setType("audio/mp3");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+"/sdcard/"+audioClipFileName));
startActivity(sendIntent);
but its not working.why is not working?how can i solve this issue?
You need use Environment and not hardcode path, for example, if your file is in sdcard root use a code like this:
File root = Environment.getExternalStorageDirectory().getPath();
String fname = "bell.mp3";
file = new File(root, fname);
Intent shareCaptionIntent = new Intent(Intent.ACTION_SEND);
shareCaptionIntent.setType("audio/mp3");
shareCaptionIntent.putExtra(Intent.EXTRA_TEXT, "YOURTEXT");
shareCaptionIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.toString()));
startActivity(Intent.createChooser(shareCaptionIntent, "Share in:"));
If a resource use this:
Uri.parse("android.resource://com.my.package/raw/" + fname);
Or resource ID
Uri.parse("android.resource://com.my.package/" + R.raw.bell.mp3);
Any error please share logcat
I have downloaded a PDF file into my application in the path getFilesDir().getPath()
When I try to open the same file using below code :
String path = getBaseContext().getFilesDir().getPath()+"/myfile.pdf";
File file = new File(path);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
I am getting The target file doesnot exist error from pdf reader.
When I browse my device for myfile.pdf path, It's located in "/sdcard/Android/data/com.***.***/files/data/com.***.***/files/myfile.pdf" .
If I hardcode the above path then also I am getting the same error.
Please let me know how to read file from that path.
use: Environment.getExternalStorageDirectory().getAbsolutePath()
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/myfile.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
I want to show user my apk file using intents, here is the code:
SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,fileName);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new
Intent(Intent.ACTION_VIEW,path);
startActivity(intent);
but it is giving me ActivityNotFoundException,I want to start the apk file so that user can install it.
Try this?
Uri path = Uri.fromFile(file);
Intent intent = new
Intent(Intent.ACTION_VIEW,path);
intent.setDataAndType(path, "application/vnd.android.package-archive");
startActivity(intent);