I am trying to create a share button that will share an Image with some extra caption text to social media.
The code I'm using now works fine with some apps, but doesn't work in other apps.
Here's the code:
String message = "hello world";
Uri uri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.loremipsum2);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/jpeg");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
I've been searching for hours what the problem may be, but none of the solutions have worked for me so far.
Apps where it does work:
-Telegram
-Drive
-Reddit
Apps where it doesn't work:
-WhatsApp
-Gmail
Error when sharing to Gmail:
Gmail:Can't find android resource
java.io.FileNotFoundException: No package found for authority: android.resource://com.test.testApp/2131820547
Error when sharing to whatsapp
Access denied finding property "ro.vendor.scroll.preobtain.enable"
I'm going crazy onto this one, I need it to work and can obviously be achieved, as many apps can share images to whatsapp.
Any ideas on how to solve it? you're welcome! :)
EXTRA_STREAM is documented to take a content: Uri. That is not what you are using. Few developers know of the android.resource scheme. Fewer still will have code that handles it when an app uses one unexpectedly in EXTRA_STREAM.
For better compatibility, use a ContentProvider and a content: Uri that points to it. For example, you could share a file with FileProvider.
Okay, so finally, after researching a lot, trust me, a lot, I've finally found an answer to my question, so I'm posting it here as it seems like a pretty basic feature to have in an app.
The solution is, as CommonsWare said, using a fileProvider.
I also used a class I found on github to manage cache usage , it is linked here, as FileProvider may not use resource URIs
The idea is, store the image in cache, then call Cache class to provide a valid URI to the image in cache, and then use that URI for the EXTRA_STREAM
Follow this guide to solve the issue: (it works in all apps I've tried so far. It should also work for other File types with some little modifications.)
SOLUTION
1-Create the class that handles cache. Here's code:
public class Cache {
public static final String TAG = Cache.class.getSimpleName();
private static final String CHILD_DIR = "images";
private static final String TEMP_FILE_NAME = "img";
private static final String FILE_EXTENSION = ".png";
private Context context;
private static final int COMPRESS_QUALITY = 100;
public Cache(Context context){
this.context = context;
}
/**
* Save image to the App cache
* #param bitmap to save to the cache
* #param name file name in the cache.
* If name is null file will be named by default {#link #TEMP_FILE_NAME}
* #return file dir when file was saved
*/
public File saveImgToCache(Bitmap bitmap, #Nullable String name) {
File cachePath = null;
String fileName = TEMP_FILE_NAME;
if (!TextUtils.isEmpty(name)) {
fileName = name;
}
try {
cachePath = new File(context.getCacheDir(), CHILD_DIR);
cachePath.mkdirs();
FileOutputStream stream = new FileOutputStream(cachePath + "/" + fileName + FILE_EXTENSION);
bitmap.compress(Bitmap.CompressFormat.PNG, COMPRESS_QUALITY, stream);
stream.close();
} catch (IOException e) {
Log.e(TAG, "saveImgToCache error: " + bitmap, e);
}
return cachePath;
}
/**
* Save an image to the App cache dir and return it {#link Uri}
* #param bitmap to save to the cache
*/
public Uri saveToCacheAndGetUri(Bitmap bitmap) {
return saveToCacheAndGetUri(bitmap, null);
}
/**
* Save an image to the App cache dir and return it {#link Uri}
* #param bitmap to save to the cache
* #param name file name in the cache.
* If name is null file will be named by default {#link #TEMP_FILE_NAME}
*/
public Uri saveToCacheAndGetUri(Bitmap bitmap, #Nullable String name) {
File file = saveImgToCache(bitmap, name);
return getImageUri(file, name);
}
/**
* Get a file {#link Uri}
* #param name of the file
* #return file Uri in the App cache or null if file wasn't found
*/
#Nullable public Uri getUriByFileName(String name) {
Context context = this.context;
String fileName;
if (!TextUtils.isEmpty(name)) {
fileName = name;
} else {
return null;
}
File imagePath = new File(context.getCacheDir(), CHILD_DIR);
File newFile = new File(imagePath, fileName + FILE_EXTENSION);
return FileProvider.getUriForFile(context, context.getPackageName() + ".provider", newFile);
}
// Get an image Uri by name without extension from a file dir
private Uri getImageUri(File fileDir, #Nullable String name) {
Context context = this.context;
String fileName = TEMP_FILE_NAME;
if (!TextUtils.isEmpty(name)) {
fileName = name;
}
File newFile = new File(fileDir, fileName + FILE_EXTENSION);
return FileProvider.getUriForFile(context, context.getPackageName() + ".provider", newFile);
}
/**
* Get Uri type by {#link Uri}
*/
public String getContentType(Uri uri) {
return this.context.getContentResolver().getType(uri);
}
}
I've done little to none modification to this class, the author is Ivan V on 12.05.2019. (Github)
This class uses a FileProvider internally. FileProvider will allow other apps to access our app files.
To configure file providers, we have to do some modification in AndroidManifest.xml
2 - Inside the <application> tag, place the following FileProvider config:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:grantUriPermissions="true"
android:exported="false"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
And create file_paths.xml in /res/xml (if the xml folder doesn't exist, create it to) with the following code:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="file_path" path="."/>
<external-files-path name="external_path" path="/" />
</paths>
3 - Create the sharing method in your activity
public void shareImage(){
Cache cache = new Cache(getActivity());
cache.saveImgToCache(BitmapFactory.decodeResource(getResources(),R.raw.loremipsum),"testImg");
Uri sendUri = cache.getUriByFileName("testImg");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "whatsAppMessage");
intent.putExtra(Intent.EXTRA_STREAM, sendUri);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
Just change all image names etc and you are ready to go!!
I hope i was helpful :)
Related
I am using an Intent to capture videos in my app. My code is similar to this (adapted from https://developer.android.com/training/camera-deprecated/photobasics):
File mVideo;
private File createVideoFile() throws IOException {
#SuppressLint("SimpleDateFormat")
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmssSSS").format(new Date());
String videoFileName = "VID_" + timeStamp + ".mp4";
File storageDir = Environment.getExternalStorageDirectory();
File movies = new File(storageDir, "Movies");
if (!movies.exists() && !movies.mkdir()) { // suprisingly even this works?!
Log.e(TAG, "could not create Movies directory");
throw new IOException();
}
mCurrentVideo = new File(movies, videoFileName);
}
private final ActivityResultLauncher<Uri> requestRecordVideoLauncher =
registerForActivityResult(new ActivityResultContracts.CaptureVideo(), success -> {
if (success) {
Log.i(TAG, "successfully recorded video")
assert(mVideo.canRead());
} else {
if (!mCurrentVideo.delete()) {
Log.w(TAG, "could not delete aborted video recording");
}
}
});
public void dispatchTakeVideoIntent() {
File videoFile;
try {
createVideoFile();
} catch (IOException ex) {
Log.i(TAG, "could not create file")
return;
}
Uri videoUri = FileProvider.getUriForFile(mContext, getPackageName() + ".provider", videoFile);
requestRecordVideoLauncher.launch(videoUri);
}
I have a file provider registered with this path:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
The major difference to the code in the developer manual is that I am using a path to public external storage. Thus, videos would usually be saved to the path /storage/emulated/0/Movies/VID_{DATE}{TIME}.
Testing on different devices and emulators with different API levels (21, 30, 33) concludes that this is a legal thing to do (it does not even need any permissions to read the generated files).
From what I have read in the developer references, with scoped storage an app can still access all media files it has previously created without requesting any permission (https://developer.android.com/training/data-storage/shared/media#storage-permission-not-always-needed). Additionally, the File API can still be used to access files in external storage (https://developer.android.com/about/versions/11/privacy/storage#media-direct-file-native).
Do you think, it is a good idea to trust that this will still work in later Android versions? Is there any other way to easily record a video to external storage?
I am trying to delete a file located at the path
/storage/714D-160A/Xender/image/Screenshot_commando.png
What I've done so far:
try{
String d_path = "/storage/714D-160A/Xender/image/Screenshot_commando.png";
File file = new File(d_path);
file.delete();
}catch(Exception e){
e.printStackTrace();
}
and the file is still at its place(Not deleted :( )
Also I've given permission in Manifest file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />
public static boolean delete(final Context context, final File file) {
final String where = MediaStore.MediaColumns.DATA + "=?";
final String[] selectionArgs = new String[] {
file.getAbsolutePath()
};
final ContentResolver contentResolver = context.getContentResolver();
final Uri filesUri = MediaStore.Files.getContentUri("external");
contentResolver.delete(filesUri, where, selectionArgs);
if (file.exists()) {
contentResolver.delete(filesUri, where, selectionArgs);
}
return !file.exists();
}
Using ContentResolver to delete media files is wrong and provides many problems for the user.
You can not delete a file on the sd-card simply by deleting its information from the ContentResolver on Android versions greater than Jelly Bean(4.3).
It works only on Android versions prior to KitKat(4.4).
That's why the Android team provided DocumentProvider.
Why contentResolver.delete(...) is wrong?
1. Fills up the sd-card
When you try to delete a media file on the sd-card by the ContentResolver on Android versions greater than 4.3, the actual media file will remain untouched because the contentResolver.delete(...) approach only removes the information (name, date, path ...) of the media and you will end up having unregistered media files on your sd-card which ContentResolver has no idea about their existence anymore and that's why you couldn't see them in your gallery and you think they've been deleted with this approach while they're still there and fill up the sd-card each time the user tries to delete a media file on the sd-card.
2. Media files (Images, videos, gifs ...) will come back to the gallery
There are many apps out there especially gallery and file manager ones that will find these unregistered media files and will add them to the ContentResolver again as of their normal behavior while the user assumes his/her unwanted media files are gone.
Sure no user wants his/her assuming deleted images or videos show up in the middle of a demonstration.
So, what's the correct approach to remove media files on the sd-card?
Well, this has already been answered here with the use of DocumentProvider.
From Android 4.4 onwards, you can't write to SD card files (except in the App directory) using the normal way. You'll have to use the Storage Access Framework using DocumentFile for that.
The following code works for me:
private void deletefile(Uri uri, String filename) {
DocumentFile pickedDir = DocumentFile.fromTreeUri(this, uri);
DocumentFile file = pickedDir.findFile(filename);
if(file.delete())
Log.d("Log ID", "Delete successful");
else
Log.d("Log ID", "Delete unsuccessful");
}
where filename is the name of the file to be deleted and uri is the URI returned by ACTION_OPEN_DOCUMENT_TREE:
private static final int LOCATION_REQUEST = 1;
private void choosePath() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent, LOCATION_REQUEST);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if (requestCode == LOCATION_REQUEST && resultCode == Activity.RESULT_OK) {
if (resultData != null) {
Uri uri = resultData.getData();
if (uri != null) {
/* Got the path uri */
}
}
}
}
Use Environment.getExternalStorageDirectory().getAbsolutePath() instead of hard coding storage path
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
File f = new File(baseDir + "/714D-160A/Xender/image/Screenshot_commando.png");
boolean d = f.delete();
Here is my code, but this is for a single file solution.
Can I share multiple files & uploads like I do for single files below?
Button btn = (Button)findViewById(R.id.hello);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/pic.png";
File file = new File(path);
MimeTypeMap type = MimeTypeMap.getSingleton();
intent.setType(type.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(path)));
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.putExtra(Intent.EXTRA_TEXT, "1111");
startActivity(intent);
}
});
Yes but you'll need to use Intent.ACTION_SEND_MULTIPLE instead of Intent.ACTION_SEND.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, "Here are some files.");
intent.setType("image/jpeg"); /* This example is sharing jpeg images. */
ArrayList<Uri> files = new ArrayList<Uri>();
for(String path : filesToSend /* List of the files you want to send */) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
files.add(uri);
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
startActivity(intent);
This could definitely be simplified but I left some lines in so you can break down each step that is needed.
UPDATE: Starting in API 24, sharing file URIs will cause a FileUriExposedException. To remedy this, you can either switch your compileSdkVersion to 23 or lower or you can use content URIs with a FileProvider.
UPDATE (to the update): Google recently announced that new apps and app updates would be required to target one of the latest versions of Android for release to the Play Store. That said, targeting API 23 or lower is no longer a valid option if you plan to release the app to the store. You must go the FileProvider route.
Here is little improved version improvised by MCeley's solution. This could be used to send the heterogeneous file list (like image, document and video at same time), for instance uploading downloaded documents, images at same time.
public static void shareMultiple(List<File> files, Context context){
ArrayList<Uri> uris = new ArrayList<>();
for(File file: files){
uris.add(Uri.fromFile(file));
}
final Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("*/*");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(Intent.createChooser(intent, context.getString(R.string.ids_msg_share)));
}
If you are sharing a file with another applications on devices running KitKat and above, you will need to provide Uri permissions.
This is how I handle multiple file sharing pre and post KitKat:
//All my paths will temporarily be retrieve into this ArrayList
//PathModel is a simple getter/setter
ArrayList<PathModel> pathList;
//All Uri's are retrieved into this ArrayList
ArrayList<Uri> uriArrayList = null;
//This is important since we are sending multiple files
Intent sharingIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
//Used temporarily to get Uri references
Uri shareFileUri;
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
//My paths are stored in SQLite, I retrieve them first
SQLiteHelper helper = new SQLiteHelper(this);
pathList = helper.getAllAttachments(viewholderID);
helper.close();
//Create new instance of the ArrayList where the Uri will be stored
uriArrayList = new ArrayList<>();
//Get all paths from my PathModel
for (PathModel data : pathList) {
//Create a new file for each path
File mFile = new File(data.getPath());
//No need to add Uri permissions for pre-KitKat
shareFileUri = Uri.fromFile(mFile);
//Add Uri's to the Array that holds the Uri's
uriArrayList.add(shareFileUri);
}
} else {
//My paths are stored in SQLite, I retrieve them first
SQLiteHelper helper = new SQLiteHelper(this);
pathList = helper.getAllAttachments(viewholderID);
helper.close();
//Create new instance of the ArrayList where the Uri will be stored
uriArrayList = new ArrayList<>();
//Get all paths from my PathModel
for (PathModel data : pathList) {
//Create a new file for each path
File mFile = new File(data.getPath());
//Now we need to grant Uri permissions (kitKat>)
shareFileUri = FileProvider.getUriForFile(getApplication(), getApplication().getPackageName() + ".provider", mFile);
//Add Uri's to the Array that holds the Uri's
uriArrayList.add(shareFileUri);
}
//Grant read Uri permissions to the intent
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
//I know that the files which will be sent will be one of the following
sharingIntent.setType("application/pdf/*|image|video/*");
//pass the Array that holds the paths to the files
sharingIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriArrayList);
//Start intent by creating a chooser
startActivity(Intent.createChooser(sharingIntent, "Share using"));
In my case the paths were stored in SQLite, but the paths can come from wherever.
/*
manifest file outside the applicationTag write these permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> */
File pictures = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//Get a top-level public external storage directory for placing files of a particular type.
// This is where the user will typically place and manage their own files,
// so you should be careful about what you put here to ensure you don't
// erase their files or get in the way of their own organization...
// pulled from Standard directory in which to place pictures that are available to the user to the File object
String[] listOfPictures = pictures.list();
//Returns an array of strings with the file names in the directory represented by this file. The result is null if this file is not a directory.
Uri uri=null;
ArrayList<Uri> arrayList = new ArrayList<>();
if (listOfPictures!=null) {
for (String name : listOfPictures) {
uri = Uri.parse("file://" + pictures.toString() + "/" + name );
arrayList.add(uri);
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_STREAM, arrayList);
//A content: URI holding a stream of data associated with the Intent, used with ACTION_SEND to supply the data being sent.
intent.setType("image/*"); //any kind of images can support.
chooser = Intent.createChooser(intent, "Send Multiple Images");//choosers title
startActivity(chooser);
}
I got this class:
import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.util.Log;
public class MediaScannerWrapper implements
MediaScannerConnection.MediaScannerConnectionClient {
private MediaScannerConnection mConnection;
private String mPath;
private String mMimeType;
// filePath - where to scan;
// mime type of media to scan i.e. "image/jpeg".
// use "*/*" for any media
public MediaScannerWrapper(Context ctx, String filePath, String mime){
mPath = "/sdcard/DCIM/Camera";
mMimeType = "jpg";
mConnection = new MediaScannerConnection(ctx, this);
}
// do the scanning
public void scan() {
mConnection.connect();
}
// start the scan when scanner is ready
public void onMediaScannerConnected() {
mConnection.scanFile(mPath, mMimeType);
Log.w("MediaScannerWrapper", "media file scanned: " + mPath);
}
public void onScanCompleted(String path, Uri uri) {
// when scan is completes, update media file tags
}
}
How to use it in the other class?
I don't know how to properly use classes, I tried but nothing is working.
I do something wrong, but I don't know what, can someone help me with this.
The Story
Before Android 4.4, we could just send a broadcast to trigger the media scanner on any particular file, or folder or even on the root of the storage. But from 4.4 KitKat, this have been fixed by the Android Developers.
Why do I say fixed? The reason is simple. Sending a broadcast using MEDIA_MOUNTED on the root directory is very expensive. Running the Media Scanner is an expensive operation and the situation gets even worse when the user has got a lot of files in the storage and deep folder structures.
Before Android 4.4
Keep it straight and simple. If you are targeting your app before Android 4.4. But keep in mind not to use it on the root directory unless absolutely necessary.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
From Android 4.4
There are two ways for you.
i) The first one is very similar to the previous example, but may not work efficiently and is not recommended too.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
ii) Now, let us move on to the most recommended and efficient solution to this problem.
Add the file paths of the files which have been updated, like this, in a String type ArrayList
ArrayList<String> toBeScanned = new ArrayList<String>();
toBeScanned.add(item.getFilePath());
Now you need to run scanFile() static method of the MediaScannerConnection class and pass the String array containing the list of all the files which have been updated and needs to be media scanned.
You can also put a listener to respond when the scanning has been finished for individual files.
String[] toBeScannedStr = new String[toBeScanned.size()];
toBeScannedStr = toBeScanned.toArray(toBeScannedStr);
MediaScannerConnection.scanFile(getActivity(), toBeScannedStr, null, new OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
System.out.println("SCAN COMPLETED: " + path);
}
});
Hey I found out how to do it with a very simple code.
Just call this line of code:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
This should trigger mediascanner.
In Android, there is a content database which is used by the media scanner to keep track of all the media content present on the device.
When Android boots up, the mediascanner service is launched and runs through the entire external storage to find if there is any new media content if it finds one then,
It adds an entry of that media content into the content database
Each entry in the content database contains metadata of the media content like Name, date, file size, type of file, etc..
So when you make a modification to a media content, you will need to update the content database also.
If the content database is not update then other applications also will not be able to access that particular media content.
Running the media scanner just updates the content database
Instead of running the media scanner, you can update the content database yourself and it should resolve the problem.
Here is an explanation on how to insert, delete, update using the content resolver. (Search for the section "Inserting, Updating, and Deleting Data")
Edit:
There is a sample code in this answer. Check for the answer by Janusz.
File file = new File(absolutePath);
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
sendBroadcast(intent);
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
Reference: http://developer.android.com/training/camera/photobasics.html#TaskGallery
The Add the Photo to a Gallery Section
As #Aritra Roy's answer, i decide to make an experiment about this issue.
What i got here are:
Intent.ACTION_MEDIA_MOUNTED and Intent.ACTION_MEDIA_SCANNER_SCAN_FILE
can accept individual file path, so sendBroadcast(new
Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(filePath)));
or sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse(filePath))); will be valid.
If you use individual file path with Intent.ACTION_MEDIA_MOUNTED on Kitkat or above, your application will still crash
If you use Intent.ACTION_MEDIA_SCANNER_SCAN_FILE or MediaScannerConnection on device lower than Kitkat, your application will not force close, but the method will just simply not working as you want.
From that experiment, i think the best method to handle is
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
MediaScannerConnection.scanFile(context, new String[]{imagePath}, null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
//something that you want to do
}
});
} else {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + imagePath)));
}
Let me know if i missed something
On my application I write a file to the internal storage as covered on android developer. Then later on I wish to email the file I wrote into the internal storage. Here is my code and the error I am getting, any help will be appreciated.
FileOutputStream fos = openFileOutput(xmlFilename, MODE_PRIVATE);
fos.write(xml.getBytes());
fos.close();
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
...
Uri uri = Uri.fromFile(new File(xmlFilename));
intent.putExtra(android.content.Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Send eMail.."));
And the error is
file:// attachment path must point to file://mnt/sdcard. Ignoring attachment file://...
I think you may have found a bug (or at least unnecessary limitation) in the android Gmail client. I was able to work around it, but it strikes me as too implementation specific, and would need a little more work to be portable:
First CommonsWare is very much correct about needing to make the file world readable:
fos = openFileOutput(xmlFilename, MODE_WORLD_READABLE);
Next, we need to work around Gmail's insistence on the /mnt/sdcard (or implementation specific equivalent?) path:
Uri uri = Uri.fromFile(new File("/mnt/sdcard/../.."+getFilesDir()+"/"+xmlFilename));
At least on my modified Gingerbread device, this is letting me Gmail an attachment from private storage to myself, and see the contents using the preview button when I receive it. But I don't feel very "good" about having to do this to make it work, and who knows what would happen with another version of Gmail or another email client or a phone which mounts the external storage elsewhere.
I have been struggling with this issue lately and I would like to share the solution I found, using FileProvider, from the support library. its an extension of Content Provider that solve this problem well without work-around, and its not too-much work.
As explained in the link, to activate the content provider:
in your manifest, write:
<application
....
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.youdomain.yourapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
...
the meta data should indicate an xml file in res/xml folder (I named it file_paths.xml):
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path path="" name="document"/>
</paths>
the path is empty when you use the internal files folder, but if for more general location (we are now talking about the internal storage path) you should use other paths. the name you write will be used for the url that the content provider with give to the file.
and now, you can generate a new, world readable url simply by using:
Uri contentUri = FileProvider.getUriForFile(context, "com.yourdomain.yourapp.fileprovider", file);
on any file from a path in the res/xml/file_paths.xml metadata.
and now just use:
Intent mailIntent = new Intent(Intent.ACTION_SEND);
mailIntent.setType("message/rfc822");
mailIntent.putExtra(Intent.EXTRA_EMAIL, recipients);
mailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
mailIntent.putExtra(Intent.EXTRA_TEXT, body);
mailIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
try {
startActivity(Intent.createChooser(mailIntent, "Send email.."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, R.string.Message_No_Email_Service, Toast.LENGTH_SHORT).show();
}
you don't need to give a permission, you do it automatically when you attach the url to the file.
and you don't need to make your file MODE_WORLD_READABLE, this mode is now deprecated, make it MODE_PRIVATE, the content provider creates new url for the same file which is accessible by other applications.
I should note that I only tested it on an emulator with Gmail.
Chris Stratton proposed good workaround. However it fails on a lot of devices. You should not hardcode /mnt/sdcard path. You better compute it:
String sdCard = Environment.getExternalStorageDirectory().getAbsolutePath();
Uri uri = Uri.fromFile(new File(sdCard +
new String(new char[sdCard.replaceAll("[^/]", "").length()])
.replace("\0", "/..") + getFilesDir() + "/" + xmlFilename));
Taking into account recommendations from here: http://developer.android.com/reference/android/content/Context.html#MODE_WORLD_READABLE, since API 17 we're encouraged to use ContentProviders etc.
Thanks to that guy and his post http://stephendnicholas.com/archives/974 we have a solution:
public class CachedFileProvider extends ContentProvider {
public static final String AUTHORITY = "com.yourpackage.gmailattach.provider";
private UriMatcher uriMatcher;
#Override
public boolean onCreate() {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "*", 1);
return true;
}
#Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
switch (uriMatcher.match(uri)) {
case 1:// If it returns 1 - then it matches the Uri defined in onCreate
String fileLocation = AppCore.context().getCacheDir() + File.separator + uri.getLastPathSegment();
ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(fileLocation), ParcelFileDescriptor.MODE_READ_ONLY);
return pfd;
default:// Otherwise unrecognised Uri
throw new FileNotFoundException("Unsupported uri: " + uri.toString());
}
}
#Override public int update(Uri uri, ContentValues contentvalues, String s, String[] as) { return 0; }
#Override public int delete(Uri uri, String s, String[] as) { return 0; }
#Override public Uri insert(Uri uri, ContentValues contentvalues) { return null; }
#Override public String getType(Uri uri) { return null; }
#Override public Cursor query(Uri uri, String[] projection, String s, String[] as1, String s1) { return null; }
}
Than create file in Internal cache:
File tempDir = getContext().getCacheDir();
File tempFile = File.createTempFile("your_file", ".txt", tempDir);
fout = new FileOutputStream(tempFile);
fout.write(bytes);
fout.close();
Setup Intent:
...
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/" + tempFile.getName()));
And register Content provider in AndroidManifest file:
<provider android:name="CachedFileProvider" android:authorities="com.yourpackage.gmailattach.provider"></provider>
File.setReadable(true, false);
worked for me.
The error is enough specific: you should use file from external storage in order to make an attachment.
If you are going to use internal storage, try to use the exact storage path:
Uri uri = Uri.fromFile(new File(context.getFilesDir() + File.separator + xmlFilename));
or additionally keep changing the file name in the debugger and just call "new File(blah).exists()" on each of the file to see quickly what exact name is your file
it could also be an actual device implementation problem specific to your device. have you tried using other devices/emulator?