问题
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());
来源:https://stackoverflow.com/questions/8080673/android-cant-share-files