For query I need get _id, last digital from path
On my android 4.4 I see in debugger that mp3 path looks like: content://com.android.providers.media.documents/document/audio:10719 and I try to get real path as:
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == REQUEST_MUSIC_GET && resultCode == RESULT_OK){
Uri urri = data.getData();
String[] musIdAr=urri.getPath().split(":");
String orderBy = MediaStore.Audio.Media.DATA + " DESC";
Cursor cr = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,"_id = ?",new String(musIdAr{musIdAr.length-1]},orderBy);
cr.moveToFirst();
String src=cr.getString(1);//real path id
cr.close();
initPlayer();
}
}
It works good,
but on android 4.3 I see that path looks like content://media/external/audio/media/900
without audio:10719 - it dosn't work
Is there universal way to get real path?
Related
my goal is to retrieve a contact's phone number after it gets picked in the contact list. This is the code I am using
private void onClick(){
Intent pickContact = new Intent(Intent.ACTION_PICK);
pickContact.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContact, CONTACT_PICK_CODE);
}
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == CONTACT_PICK_CODE) {
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
if (cursor.moveToFirst()) {
int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(phoneIndex);
}
c.close();
}
}
which works fine if the standard Android contacts app is being used, but crashes when the app "Simple Contacts" is being used.
In particular, the error happens because phoneIndex is -1.
How to make it work even if the user chooses to use another contact list app?
For reference, this is the cursor when the contact gets selected from Simple Contacts.
As you can see it contains only 36 columns, while using the standard contact list app it has 84 columns, one of which is ContactsContract.CommonDataKinds.Phone.NUMBER
I'm using an image picker intent, to allow users to choose an image from their gallery, I get it's path, and pass it then to a 3rd library.
It's working fine for most of the cases, but if I picked up an image from Google Photos (an image that is stored online) I get a null path, though that I get valid URI for both of the working and not working images.
Here is my Intent call:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
and here is onActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri = data.getData();
Log.e(getClass().getName(),"file uri = " + uri);
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(uri, projection,
null, null, null);
if(cursor == null) return;
Log.e(getClass().getName(),"file cursor = " + cursor);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
Log.e(getClass().getName(),"file columnIndex = " + columnIndex);
cursor.moveToFirst();
// The crash happens here
String photoPath = cursor.getString(columnIndex);
Log.e(getClass().getName(),"file photo path = " + photoPath);
cursor.close();
cropImage(photoPath);
}
And here are the logs for working and not-working image:
Working image:
file uri = content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F105681/ORIGINAL/NONE/187859359
file cursor =
android.content.ContentResolver$CursorWrapperInner#8953964
file columnIndex = 0
file photo path = /storage/emulated/0/DCIM/Camera/IMG_20190523_184830.jpg
Not working image:
file uri =
content://com.google.android.apps.photos.contentprovider/0/1/mediakey%3A%2Flocal%253A4574915c-b4ac-40af-bc08-b1004670cab2/ORIGINAL/NONE/477302338
file cursor =
android.content.ContentResolver$CursorWrapperInner#59448a4
file columnIndex = 0
file photo path = null
If there isn't way to avoid that error, is there a way instead to hide photos that are stored online and only show local ones?
The technique in your question has (at least) three problems:
Not every MediaStore entry has a value for DATA, as you're seeing
Not every non-null DATA value represents a filesystem path that you can access, as the MediaStore can get to content that you can't
The DATA column is not available on Android Q and higher
In your case, the uCrop library accepts a Uri. Well-written Android libraries know how to handle a Uri, so you can just hand the Uri over to the library and it will take it from there.
In my project i need a way of get the path of several images selecteds from the gallery.
I´m using this library MultipleSelectImages
It´s apparently work fine, but in the onActivityResult i need the array with the path of each image, however the result I get is this:
Paths: [com.darsh.multipleimageselect.models.Image#1e6d3057, com.darsh.multipleimageselect.models.Image#33824744]
...when i need the real path (/storage/emulated/0/DCIM/Camera/20150426_110936.jpg)
Reading the doc of the library don´t found solution.
This is the onActivityResult method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data != null) {
//The array list has the image paths of the selected images
ArrayList<Image> images = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
Log.i("myLogs", "Paths:" + " " + images);
}
}
...Where "image" it´s imported from the library
import com.darsh.multipleimageselect.models.Image;
I´m not using EXTRA_ALLOW_MULTIPLE because need that the app works in android api 16 version
Thanks in advanced.
Try to use below code;
onActivityResult();-
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imgPath = cursor.getString(columnIndex);
cursor.close();
I am working on one sample application just to insert, update and delete the native android contact. I am able to successfully insert, update and delete the contact. But the problem in updating the contact photo. Below images are the observation where the same contact having two different issue.
After updating the contact, first image is still displaying the old image. But where as when i view the full details i am able to view the newly updated contact image as shown in the second image. Below is the code for updating the contact image.
mBitmap =getAllowedPhotoBitmap(photo);
mBitmap = ThumbnailUtils.extractThumbnail(mBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
if(mBitmap!=null){ // If an image is selected successfully
mBitmap.compress(Bitmap.CompressFormat.PNG ,100, stream);
op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
op.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[{String.valueOf(native_contactid), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE});
op.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, stream.toByteArray());
ops.add(op.build());
}
What is the problem and where i am going wrong?
Here is a open source app that does that: https://github.com/heinrisch/Contact-Picture-Sync/blob/master/src/heinrisch/contact/picture/sync/ContactHandler.java
this file will help you set Image for contact with Contact ID
https://github.com/heinrisch/Contact-Picture-Sync/blob/master/src/heinrisch/contact/picture/sync/ContactHandler.java
void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
// Check for the request code, we might be usign multiple
if (requestCode == PICK_CONTACT_REQUEST) {
Uri contactUri = data.getData();
String[] projection = {Phone.CONTACT_ID,Phone.NUMBER,ContactsContract.Data.RAW_CONTACT_ID,ContactsContract.Data._ID };
Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
cursor.moveToFirst();
int columcontactID = cursor.getColumnIndex(Phone.CONTACT_ID);
String contactID = cursor.getString(columcontactID);
Bitmap item = (imgBg.getVisibleRectangleBitmap());
setContactPicture(AtWallpaperDetails.this, contactID, item);
}
}
}
I am using a contact picker as follows:
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT)
What I want to know is whether the last path segment of the returned URI is the CONTACT_ID or the RAW_CONTACT_ID.
In case it is the CONTACT_ID, how do I retrieve all RAW_CONTACT_IDs from this contact?
You will get CONTACT_ID as the return data.
In case if you need to get the list of all the RAW_CONTACT_ID of the contact here is what you can include in
#Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == 1) && (resultCode == RESULT_OK)){
Uri contactData = data.getData();
// This gives the value of Contact URI
Cursor c = managedQuery(RawContacts.CONTENT_URI, new String[] {RawContacts._ID}, RawContacts.CONTACT_ID + " = " + contactData.getLastPathSegment(), null, null);
// This query would give you list of Raw_COntact_ID for the added contact
}
}
Do you need to use the CONTACT_ID ?
Otherwise, I recommend you use LOOKUP_KEY instead.
See 1 and 2