I am trying to retrieve the images from Mediastore.Images.Media I am always getting the cursor to be null. Please suggest how to solve the issue. Thanks in advance
Uri contenturi = Images.Media.getContentUri("phoneStorage");
String[] mprojection = { MediaStore.Images.Media.DATA };
String mselection = null;
String[] mselectionArgs = null;
String msortOrder ="date_added DESC";
Cursor mcursor = managedQuery(contenturi, mprojection, mselection, mselectionArgs, msortOrder);
When I am checking it says the mcursor to be null, I have tried changing the uri to external storage also tried with no sort order. But still its returning null. Please help
buddy try this code,
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,getResources().getString(R.string.accept)),1);
Now just use onActivityResult method to get image from Gallery.
Related
I am trying to get the path of an image when the user pick from the gallery (with intent)
It's been working ok, since some users noticed that could not do it witn Android 6.0.
I have tried different things, and some solutions works in the emulator with Android 6.0 but not in my Xiamoi with Android 6.1.
This both solutions works in the emulator (6.0) and Android 4.4 physycal device.
public String getRealPathFromURI(Activity context, Uri contentURI) {
String[] projection = { MediaStore.Images.Media.DATA };
#SuppressWarnings("deprecation")
Cursor cursor = context.managedQuery(contentURI, projection, null,
null, null);
if (cursor == null)
return null;
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (cursor.moveToFirst()) {
String s = cursor.getString(column_index);
// cursor.close();
return s;
}
// cursor.close();
return null;
}
and the other similar:
private static String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
But in my Xiaomi 6.1 the cursor is null. But I can get the real path from:
private static String getRealPathFromURI(Context context, Uri contentUri) {
return contentUri.getEncodedPath();
}
Any help?
Thank you!
EDIT:
I'm asking for choose an image in this way:
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK); //ACTION_GET_CONTENT
// Always show the chooser (if there are multiple options available)
launchForResult(Intent.createChooser(intent, "Select Picture"), SELECT_FILE);
I'm asking for choose an image in this way:
First, use ACTION_GET_CONTENT to pick by MIME type.
Second, whatever activity that responds to ACTION_GET_CONTENT (or ACTION_PICK) does not need to return a Uri that the MediaStore knows about. In fact, most will not. They can return a Uri that points to anything, including things that are not files.
So, get rid of all of your broken getRealPathFromURI() code. Use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri, and use that stream.
I need a conversation cursor which does not have given addresses.
This is how i am getting the cursor :
final Uri inboxURI = Uri.parse("content://mms-sms/conversations/");
final String[] projection = new String[] { Sms._ID, Sms.THREAD_ID, Sms.ADDRESS, Sms.BODY, Sms.DATE, Sms.READ};
Cursor cursor = cr.query(inboxURI, projection, Sms.ADDRESS+" NOT IN (?)", new String[]{"456454"}, Telephony.Sms.Inbox.DEFAULT_SORT_ORDER);
This gives me an empty cursor. However this works fine if null is given as selection and selection_args.
Is this correct way?
Thanks in advance.
The code below will return all image from the SD card. But, I need to modify it so that it only display images from other folders.
Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver mContentResolver = mContext.getContentResolver();
Cursor mCursor = mContentResolver.query(mImageUri, null, null, null, null);
I have tried this:
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Testing/";
Uri uri = Uri.parse(path);
Cursor mCursor = mContentResolver.query(Uri, null, null, null, null);
and am getting an error. Any help would be appreciated.
Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
// field data which u need
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED};
Cursor mCursor = mContentResolver.query(mImageUri, columns, MediaStore.Images.Media.DATA + " like ? ",new String[] {"%/YourFolderName/%"}, null);
ArrayList<String> fileNames =null
File path =
File(getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).path,
"Your Folder Name")
if (path.exists()) {
fileNames = path.getList() //you may need to to adjust the Data type of fileNames
}
So, now you have list of images path that you can pass on to recycler view using recycler view Adapter.
I query the phone's calllog into a ListView. So when the user long clicks an item, a dialog comes up with options, including "View contact". To be able to view the contact the intent needs the contact id.
My problem is that I not always get to see the right contact. I click on Peter, and Peter's contact sheet comes up. I click on Sarah, and Jason's contact sheet comes up.
I must have been using this code the wrong way. Please help.
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
Cursor cursor = contentResolver.query(uri, new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);
if(cursor!=null) {
while(cursor.moveToNext())
{
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
contactid2 = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
cursor.close();
}
Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + contactid2));
startActivity(intent_contacts);
Maybe what I need is not the PhoneLookup._ID, but some other ID.
I also tried Felipe's answer here, same happens.
Edit:
on a HTC Desire HD (2.3.5) I get the proper contacts in 99% of the
cases.
on a ZTE Blade (2.2) I get the proper contacts in 60% of the cases.
on a Samsung Galaxy Ace (2.3.3) I get the proper contacts in 5% of the cases.
What the hell is going on???
After digging myself into theme, I found sven's help on googlegroups. The problem was that I was using a depracated intent. I read through many pages on the developer site, I must have missed it somehow.
I also post the full code.
contactid26 = null;
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phonenumintolist));
Cursor cursor =
contentResolver.query(
uri,
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
null,
null,
null);
if(cursor!=null) {
while(cursor.moveToNext()){
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
contactid26 = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
cursor.close();
}
if (contactid26 == null) {
Toast.makeText(DetailedCallHistory.this, "No contact found associated with this number", Toast.LENGTH_SHORT).show();
}
else {
Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactid26)));
//Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + contactid26));
startActivity(intent_contacts);
}
Here in this case i am going to show you that how to get the ID, and
Name of any contact no, which you have enter and want to search, For
Example if you enter a no and want to search its Name and id then use
this code, it is working 100%, if you want further modification in
this, then you can tell me
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phnno.getText().toString()));//phone no
String[] proj = new String[]
{///as we need only this colum from a table...
Contacts.DISPLAY_NAME,
Contacts._ID,
};
String id=null;
String sortOrder1 = StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor crsr = getContentResolver().query(lookupUri,proj, null, null, sortOrder1);
while(crsr.moveToNext())
{
String name=crsr.getString(crsr.getColumnIndex(Contacts.DISPLAY_NAME));
id = crsr.getString(crsr.getColumnIndex(Contacts._ID));
Toast.makeText(this,"Name of this cntct is : " +name +"\n id is : "+id ,Toast.LENGTH_SHORT).show();
}
crsr.close();
I'm modifying my app to store information on contacts using LOOKUP_KEY instead of _ID as suggested by the API docs. The only problem I'm having is that I'm no longer able to load the contact's photo.
The problematic code is this one:
InputStream s = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), contactUri);
This is returning the following error: java.lang.IllegalArgumentException: URI: content://com.android.contacts/contacts/lookup/1424i118.2312i1220228108/photo
The contactUri that I am using as argument is acquired by the following: Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, contact_key);
and in this example, contact_key is 1424i118.2312i1220228108
Based on the API docs, this helper method should work with both CONTENT_URI or CONTENT_LOOKUP_URI, which I am using.
Any ideas? Thanks.
For anyone with a similar problem, this did the trick for me:
public Bitmap getPhoto(Uri uri){
Bitmap photoBitmap = null;
String[] projection = new String[] { ContactsContract.Contacts.PHOTO_ID };
Cursor cc = getContentResolver().query(uri, projection, null, null, null);
if(cc.moveToFirst()) {
final String photoId = cc.getString(cc.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
if(photoId != null) {
final Cursor photo = managedQuery(
Data.CONTENT_URI,
new String[] {Photo.PHOTO},
Data._ID + "=?",
new String[] {photoId},
null
);
// Convert photo blob to a bitmap
if(photo.moveToFirst()) {
byte[] photoBlob = photo.getBlob(photo.getColumnIndex(Photo.PHOTO));
photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length);
}
photo.close();
}
}
cc.close();
return photoBitmap;
}