Android: can't share files - android

I have a problems, when I'm trying to share files via Bluetooth, Dropbox, etc.
Here's my code:
Intent intent = new Intent(Intent.ACTION_SEND);
File file = new File(opt.getPath());
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
intent.setType(mimetype);
String path = "file://" + file.getAbsolutePath();
intent.putExtra(Intent.EXTRA_STREAM, path);
startActivity(Intent.createChooser(intent, "Choose File"));
For example, when I select an image, getFileExtensionFromUrl returns empty string.
And when mimetype is correct(application/pdf for example), I also can't share file(I've getting message "Unsupported file type"). What's wrong am I doing?
Update
I partially solved this problem by myself.
Here's the code:
Intent intent = new Intent(Intent.ACTION_SEND);
File file = new File(opt.getPath());
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(file.getAbsolutePath());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
intent.setType(mimetype);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(Intent.createChooser(intent, "Choose File"));
But getFileExtensionFromUrl function for example still returns empty string for video(tests on *.wmv)
Update. Solved that promlem by using this code:
int dotPos = file.getName().lastIndexOf(".")+1;
String ext = file.getName().substring(dotPos);
and removing
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(file.getAbsolutePath());

Related

Video sent to whatsapp not playing

In my app there is a feature where the user can share a video with the app of their choice. The code is fairly straightforward (where mediaPath is a variable of type String which is a path to a valid video):
File media = new File(mediaPath);
Uri uri = FileProvider.getUriForFile(context, getString(R.string.file_provider_authority), media);
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("video/*");
String title = getString(R.string.share_video_title);
Intent chooser = Intent.createChooser(share, title);
if (share.resolveActivity(context.getPackageManager()) != null) {
startActivity(chooser);
}
Sharing works perfectly on gmail (for example) and seems to work fine on whatsapp as well. It compresses the video and uploads it. The recipient gets the video and is able to see a thumbnail and download it. However they cannot play the video.
I finally found the solution is here
public void shareVideoWhatsApp() {
Uri uri = Uri.fromFile(v);
Intent videoshare = new Intent(Intent.ACTION_SEND);
videoshare.setType("*/*");
videoshare.setPackage("com.whatsapp");
videoshare.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
videoshare.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(videoshare);
}
Refrence
Are you try this:
String path = ""; //should be local path of downloaded video
ContentValues content = new ContentValues(4);
content.put(MediaStore.Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, path);
ContentResolver resolver = getApplicationContext().getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("video/*");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Hey this is the video subject");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Hey this is the video text");
sharingIntent.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(sharingIntent,"Share Video");

share file over bluetooth opp in Android N

What i am trying is to share a file over bluetooth. I have tried below two methods to pass the file name to the ACTION_SEND intend. share activity is pop'ing up and when i touch the connected bluetooth device, i get a toast saying Bluetooth share: File Unknown file not sent. Both the method fails.
public void pushFileOverOpp(String filename) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setPackage("com.android.bluetooth");
intent.setType("audio/mp3");
File f = new File(Environment.getExternalStorageDirectory(), "images");
File sample = new File(f, "sample.mp3");
Uri u = Uri.parse(sample.toString());
intent.putExtra(Intent.EXTRA_STREAM, u);
mContext.startActivity(intent);
}
Error , Log-
OppService: URI : /storage/emulated/0/images/sample.mp3
OppService: HINT : null
OppService: FILENAME: null
OppService: MIMETYPE: audio/mp3
File f = new File(mContext.getFilesDir(), "images");
File sample = new File(f, "sample.mp3");
Uri u = FileProvider.getUriForFile(mContext,
BuildConfig.APPLICATION_ID + ".provider", sample);
intent.putExtra(Intent.EXTRA_STREAM, u);
Error, Log-
OppService: URI : content://com.example.com.test.provider/tester/images/sample.mp3
OppService: HINT : null
OppService: FILENAME: null
I have checked the android source code, This error comes when filename is null. Log also says filename is null. But i could not figure out the exact reason. Could someone Please help me out here, what is wrong with my code.
After some study i understood the problem. There were two issues-
xml tag for external storage(/sdcard/) directory was wrong in xml file.
I changed as below.
<root-path
name="root"
path="/" />
URI permission was not granted
mContext.grantUriPermission("com.android.bluetooth", u,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
After modifying with above lines of code, File share is working !
full working code-
public boolean pushFileOverOpp(String filename) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("*/*"); // supports all mime types
intent.setPackage("com.android.bluetooth"); //bluetooth package name, default opp
File folder = new File(Environment.getExternalStorageDirectory(), "images");
File file = new File(folder, filename);
if (!file.exists()) {
Logger.e("No such file " + filename + " exists!");
return false;
}
Uri u = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", file);
intent.putExtra(Intent.EXTRA_STREAM, u);
mContext.grantUriPermission("com.android.bluetooth", u,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
Logger.d("Sharing file over bluetooth " + folder.toString());
mContext.startActivity(intent);
return true;
}
Thanks.
Please refer this code , it works and share the files using createChooser method.
ArrayList<Uri> arrayList2 = new ArrayList<>();
String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() +
"/NewCallLogs/audio.mp3");
File files = new File(MEDIA_PATH);
Uri u = Uri.fromFile(files);
arrayList2.add(u);
Intent share = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
share.setData(Uri.parse("mailto:"));
share.setType("audio/mpeg");
share.putExtra(android.content.Intent.EXTRA_STREAM, arrayList2);
try {
startActivity(Intent.createChooser(share, "Share..."));
// getActivity().finish();
Log.i("Finished sharing.", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "nothing shared.", Toast.LENGTH_SHORT).show();
}
For sharing the file only in bluetooth
ArrayList<Uri> arrayList2 = new ArrayList<>();
String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() +
"/NewCallLogs/audio.mp3" );
File files = new File(MEDIA_PATH);
Uri u = Uri.fromFile(files);
arrayList2.add(u);
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setData(Uri.parse("mailto:"));
share.setType("audio/mpeg");
share.setPackage("com.android.bluetooth");
share.putExtra(android.content.Intent.EXTRA_STREAM, arrayList2);
startActivity(share);

Android - How do I open a file in another app via Intent?

I'm trying to open a file using another app, i.e. opening a .jpg with Gallery, .pdf with Acrobat, etc.
The problem I'm having with this is when I try to open the file in the app, it only opens the chosen app instead of opening the file within the app. I tried following Android open pdf file via Intent but I must be missing something.
public String get_mime_type(String url) {
String ext = MimeTypeMap.getFileExtensionFromUrl(url);
String mime = null;
if (ext != null) {
mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
}
return mime;
}
public void open_file(String filename) {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), filename);
// Get URI and MIME type of file
Uri uri = Uri.fromFile(file).normalizeScheme();
String mime = get_mime_type(uri.toString());
// Open file with user selected app
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
intent.setType(mime);
context.startActivity(Intent.createChooser(intent, "Open file with"));
}
As far as I can tell, it returns the right URI and MIME type:
URI: file:///storage/emulated/0/Download/Katamari-ringtone-985279.mp3
MIME: audio/mpeg
Posting my changes here in case it can help someone else. I ended up changing the download location to an internal folder and adding a content provider.
public void open_file(String filename) {
File path = new File(getFilesDir(), "dl");
File file = new File(path, filename);
// Get URI and MIME type of file
Uri uri = FileProvider.getUriForFile(this, App.PACKAGE_NAME + ".fileprovider", file);
String mime = getContentResolver().getType(uri);
// Open file with user selected app
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, mime);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
I used this piece of code and it worked perfectly fine on android 6 and below not tested on the higher version
public void openFile(final String fileName){
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(android.os.Environment.getExternalStorageDirectory()
.getAbsolutePath()+ File.separator+"/Folder/"+fileName));
intent.setDataAndType(uri, "audio/mpeg");
startActivity(intent);
}

How to attach a image and a .txt(multiple files) file to gmail through Coding?

I am trying to attach an image file and .txt file but only one file is attaching one is not attaching how to attach both the files.
if i use ACTION_SEND_MULTIPLE then the app gets force close.
HERE IS MY CODE:
public static Intent getSendEmailIntent(Context context, String email,
String subject, String body, String fileName) {
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
// Explicitly only use Gmail to send
emailIntent.setClassName("com.google.android.gm",
"com.google.android.gm.ComposeActivityGmail");
emailIntent.setType("*/*");
// Add the recipients
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,
new String[] { email }
);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
// Add the attachment by specifying a reference to our custom
// ContentProvider
// and the specific file of interest
emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/"+ "anything.txt"));
emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/"+ "anything.jpg"));
return emailIntent;
}
If I put a .jpg file at last before return it is attaching. But if I put a .txt file at last then it is attaching. I want to attach both files pls help.
You can just use the code below and change file path's to your files.
ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(Uri.fromFile(new File("/mnt/sdcard/Medplann/IMG.jpg"))); // Add your image URIs here
imageUris.add(Uri.fromFile(new File("/mnt/sdcard/Medplann/Report.pdf")));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));
Read this page : send

Convert string to file and send this image through email attachment

I get an image from a PHP server as a string (this image), and I want to send this image by e-mail as a attachment.
When I try to send e-mail, then the attachment is papers but only plain text is displaying at the receiver end. How do I send the e-mail correctly?
This is my code:
Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);
picMessageIntent.setType("image/jpeg");
String img_source = listBuzzInfoBean.get(photo).getBuzzImage();
File downloadedPic = new File(Environment.getExternalStorageDirectory(), img_source + ".jpg");// Art_Nature
Log.d("++++++++++++++", img_source);
Uri myUri = Uri.fromFile(downloadedPic);
picMessageIntent.putExtra(Intent.EXTRA_STREAM, myUri); //screenshotUri
startActivity(Intent.createChooser(picMessageIntent, "Share image using"));
I used the following code to handle this. I have noticed that differences between Gmail/Mail apps and how they handle this intent.
Also this post seems to have a similar problem.
Android: Intent.ACTION_SEND with EXTRA_STREAM doesn't attach any image when choosing Gmail app on htc Hero
public static Intent createEmailWithAttachmentIntent(String[] addresses,
String[] ccAddresses, String subject, String contentType,
String filename, String messageBody) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(Intent.EXTRA_EMAIL, addresses);
i.setType(contentType);
if(filename != null && !filename.equals(""))
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filename)));
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, messageBody);
if (ccAddresses != null && ccAddresses.length > 0)
i.putExtra(Intent.EXTRA_CC, ccAddresses);
return i;
}

Categories

Resources