ContentResolver -- how to get file path of attachment - android

I need help in accessing the attachment received in an email.
I have setup intent filter for my activity and on clicking the attachment in the gmail app, my activity is launched with intent containing following Uri.
content://gmail-ls/xxxx#gmail.com/messages/2407/attachments/0.1/BEST/false
I have tried following query and filename returned is null.
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
if (c.moveToFirst()) {
fileName = c.getString(0);
}
Anyone, please let me know how can I access the file received in the attachment ?
Thanks.

Anyone, please let me know how can I access the file received in the attachment ?
There isn't necessarily a file at all, let alone one that you have direct access to. Gmail, in particular, had better be storing its attachments in a place where other apps cannot access them directly.
To use the attachment, use the Uri with a ContentResolver to get an InputStream on it.

Related

Android get image path after intent

I made an app. It has 2 options for uploading photo:
1) by taking a photo using camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
2) by picking from gallery
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
My problem is getting file path after these intents in onActivityResult.
Is there any methods to get those paths for new File(path) , that also takes care of sdk level changes? For example till KitKat is 1 type of file system , after KitKat is other type.
My problem is getting file path after these intents in onActivityResult.
There will be no file path for the ACTION_IMAGE_CAPTURE approach, because you did not provide EXTRA_OUTPUT. If you do provide EXTRA_OUTPUT, then you already know what the file path is.
There is no file path for ACTION_PICK, insofar as there is no requirement that what the user picks be in a file that you have access to. For example, it could be an image on removable storage. Use a ContentResolver and methods like openInputStream() to get the content represented by the Uri that you are given.
If you can able to get path from camera then for Intent.ACTION_PICK you can directly get uri of image using data.getData() and then you can get file path using this method(as suggested in the link provided by Gennadii Saprykin
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx); }

Different URI from ACTION_SEND intent

I've included in my app the possibility to insert content with the "share with" button from the phone gallery, but in different devices i have different results. For example, if i retrieve the uri of the content from the Intent with the code
val fileUri: Uri = intent.getParcelableExtra(Intent.EXTRA_STREAM).asInstanceOf[Uri]
in galaxy S3 I have: content://media/external/images/media/812
and in nexus 7 I have: file:///storage/sdcard0/DCIM/Camera/ContactPhoto-IMG_20131119_173230.jpg
if i use the function:
val projection: Array[String] = Array(MediaColumns.DATA)
val cursor :Cursor= act.getContentResolver.query(uri,projection,null,null,null)
val column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA)
cursor.moveToFirst()
cursor.getString(column_index)
in this two different type of Uri, the second one give me error because the cursor is null
The problem is that sometimes the action_send is coming with a file:\ URI, and sometimes with a content:\ URI. And I need to spot file:\ and convert it to a content one.
What can I do to have the same Uri structure in all the devices?
Other apps are welcome to send whatever Uri values that they want. If your <intent-filter> says that you support those schemes, you will get those Uri values and have to deal with them.
Note that you should not be using your code snippet anyway. There is no requirement that any Uri that is given to you be in the MediaStore.

Is there a ContentResolver to obtain a File instead of a FileDescriptor?

I am using an intent to invoke a file chooser that is external to my app. Some of the file chooser applications return an Uri of scheme "content".
I need to obtain the last modified date of the chosen object. How do I do that when the scheme is "content"? I didn't find an appropriate API.
There is some API that returns a FileDescriptor. But I don't get the last modified date from a FileDescriptor. Any help appreciate.
Best Regards
In general you can't do what you want - there is no API to get a File for an item described by a "content" URI because a content URI does not have to correspond with a file anyway.
In practice it is possible to get a File path for some content URIs:
as you describe, sometimes you can get lucky, and manipulate the content uri by changing the content scheme to a file scheme
If the content URI came from the Media store, then you can do a query to get the file path
public static String getPathnameFromMediaUri(Activity activity, Uri contentUri)
{
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = activity.managedQuery(contentUri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
There are a whole host of other questions asking pretty much the same thing that provide further ideas (or slightly different working of the same ideas)
Android file chooser
How to extract the file name from URI returned from Intent.ACTION_GET_CONTENT?
URI from Intent.ACTION_GET_CONTENT into File

How to Delete a MMS message from a particular number in Android?

I want to delete a MMS message of a particular phone number in android. How can i do it?
I am using content://mms-sms/conversations to get list of MMS messaged and tried with the following query:
this.getContentResolver().delete(
Uri.parse("content://mms-sms/conversations/" + ThreadId),
"address=?", new String[] { "contact number" });`
But it works only for sms and not for mms. Can someone tell me what can we do to delete particular MMS?
Thanks
Please check below detailed Tutorial link for Read,Write,Delete and update MMS in Android
MMS in Android. Part 2. Working with MMS storage
Also Try this
Uri msgUri = ContentUris.withAppendedId(Mms.Inbox.Content_Uri,"1");
SqliteWrapper.delete(this, getContentResolver(), msgUri, null, null); (or) getContentResolver().delete(msguri, null, null);
Reference
Edit:
here is the method syntax
public static Uri withAppendedId (Uri contentUri, long id)
and Uri is the MMS inbox storage url in content provider
for example SMS Inbox Uri is "content://sms/inbox"
also read this page for MMS storage Uri info given here
Save/Create MMS in inbox Android

Android list all images available

I am making an application which requires me to list all the images available on the SD-Card of the phone.
I tried querying the ContentResolver way i.e.
Cursor image = getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, new String[]{Images.Media._ID,Images.Media.DATA,Images.Media.DISPLAY_NAME}, null, null, null);
but without any result.
Is there any way i can get the list or if that's not possible then is there any possible intent (e.g. PICK) by which I can allow the user to select a file and then access the path of the file the user selected??
//where contextObject is your activity
ContentResolver cr = contextObject.getContentResolver();
String[] columns = new String[] {
ImageColumns._ID,
ImageColumns.TITLE,
ImageColumns.DATA,
ImageColumns.MIME_TYPE,
ImageColumns.SIZE };
cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
columns, null, null, null);
My code is pretty much like yours (except broken down) and it works. You don't need to go about asking the Gallery Intent for stuff, it should work. My two guesses are:
1) Make sure your USB storage is not mounted, if it is you'll see no external images.
2) Maybe a permissions issue? Try adding GLOBAL_SEARCH permission to see if that helps at all.
You could use the gallery activity to select images, something like this:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);
in the callback for the activity the file uri will be in the intent parameter

Categories

Resources