Invalid URI exception in DocumentsContract.getDocumentId (uri) - android

I am trying to get the document id of the image which I saved, I am saving the image file to the following external storage in the device.
file:///storage/emulated/0/Pictures/BeautifulFaces/kvh_20160420T225141.jpg
After querying querying
public String checkIfImageIsRotated(Uri image_uri){
Log.i(LOG_TAG, ">>>>> START ");
int rotation =0;
String[] selection = new String[] { MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.ORIENTATION};
String selectionArgs = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " = ?";
String[] args = new String[]{FOLDER_NAME};
String sortOrder = MediaStore.Images.Media.DATE_TAKEN + " DESC";
ContentResolver content = mContext.getContentResolver();
Cursor mediaCursor = content.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
selection ,
selectionArgs,
args,
sortOrder);
//select of columns
//MediaStore.Images.ImageColumns.ORIENTATION
//MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME
if (mediaCursor != null && mediaCursor.getCount() !=0 ) {
if(mediaCursor.moveToNext()){
Log.i(LOG_TAG, ">>>>> media cursor not null ");
String orientation = mediaCursor.getString(4);
String bucket_display_name = mediaCursor.getString(1);
image_data_path = mediaCursor.getString(2);
Log.i(LOG_TAG, ">>>>> rotation - " + orientation + ", bucket display name - " + bucket_display_name + ", data - " + image_data_path);
}
mediaCursor.close();
}
Log.i(LOG_TAG, ">>>>> END ");
return image_data_path;
}
image_data_path is
/storage/emulated/0/Pictures/BeautifulFaces/kvh_20160420T225141.jpg
Now when I am trying to do DocumentsContract.getDocumentId(uri) where uri is either the file uri mentioned at the top or image_data_path returned from the cursor, in both cases I am getting the following error.
Caused by: java.lang.IllegalArgumentException: Invalid URI: file:///storage/emulated/0/Pictures/BeautifulFaces/kvh_20160420T225141.jpg
or
Caused by: java.lang.IllegalArgumentException: Invalid URI: /storage/emulated/0/Pictures/BeautifulFaces/kvh_20160420T225141.jpg
Cannot understand why Document.getDocumentId is not able to parse the Uri.
Thanks

Related

How to use limit and offset to restrict the data by content resolver (android)?

Uri uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.MediaColumns._ID, MediaStore.MediaColumns.SIZE, MediaStore.Images.ImageColumns.DATE_MODIFIED};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Bundle bundle = new Bundle();
// Sort function
bundle.putStringArray(
ContentResolver.QUERY_ARG_SORT_COLUMNS,
new String[]{MediaStore.Images.ImageColumns.DATE_MODIFIED}
);
bundle.putInt(
ContentResolver.QUERY_ARG_SORT_DIRECTION,
ContentResolver.QUERY_SORT_DIRECTION_DESCENDING
);
bundle.putInt(ContentResolver.QUERY_ARG_LIMIT, limit);
bundle.putInt(ContentResolver.QUERY_ARG_OFFSET, offset);
cursor = this.getContentResolver().query(uri, projection, bundle, null);
} else {
cursor = this.getContentResolver().query(uri, projection,
null, null
, MediaStore.MediaColumns.DATE_MODIFIED + " DESC " + " LIMIT " + limit + " OFFSET " + offset, null);
}
I am using this query to get images restricted by limit and offset. Though no matter what, this is always returning a cursor count of 4546. that means the data is not at all limited and the query is not working properly.

Android: get all contacts by IM

I want to query to Contacts content provider such that if a contact has IM whose type is equal to "XYZ".
I tried below way but I am not getting any result:
Uri uri1 = ContactsContract.Contacts.CONTENT_URI;
String[] projection1 = null;
String selection1 = null;
String[] selectionArgs1 = null;
String sortOrder1 = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
Cursor cursor1 = context.getContentResolver().query(uri1, projection1, selection1, selectionArgs1, sortOrder1);
if (cursor1 != null && cursor1.getCount() > 0) {
while (cursor1.moveToNext()) {
int contactId = Integer.parseInt(cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts._ID)));
Uri uri2 = ContactsContract.Data.CONTENT_URI;
String[] projection2 = null;
String selection2 = ContactsContract.CommonDataKinds.Im.PROTOCOL + " = ? AND " + ContactsContract.Contacts._ID + " = ? ";
String[] selectionArgs2 = new String[]{"XYZ", contactId + ""};
String sortOrder2 = null;
Cursor cursor2 = context.getContentResolver().query(uri2, projection2, selection2, selectionArgs2, sortOrder2);
if (cursor2 != null && cursor2.getCount() > 0) {
while (cursor2.moveToNext()) {
Log.i(TAG, "Name: " + cursor2.getString(cursor2.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
}
DatabaseUtils.dumpCursor(cursor2);
}
}
cursor1.close();
}
I am not getting any log with above code.
PS: I am not using built in protocols like AIM, Windows Live, Yahoo or skype. Its my custom Protocol, say it "XYZ".
For this, you need to query the ContactsContract.Data.CONTENT_URI and with the mime type as IM and then the label or type field (not sure) holds that which type of IM like you said 'XYZ' and in the value column you will get the value like a username.
There is a foreign key in this table which is linked to raw contact id of raw_contacts table.
UPDATE
Cursor cursor = getActivity().getApplicationContext().getContentResolver().query(
ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.MIMETYPE + "=?", new String[]{ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE}, null);
if(cursor!=null) {
cursor.moveToFirst();
do {
String value = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
//Types are defined in CommonDataKinds.Im.*
int imppType = cursor
.getInt(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
//Protocols are defined in CommonDataKinds.Im.*
int imppProtocol = cursor
.getInt(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Im.PROTOCOL));
//and in this protocol you can check your custom value
}while (cursor.moveToNext());
cursor.close();
}
Thanks
I have stumbled upon the same problem. Turns out that the protocol should be an Int instead of a String. In case of being a custom one you should use ContactsContract.CommonDataKinds.Im.PROTOCOL_CUSTOM which is an alias for -1.

Best way to get all documents from mobile?

I want to get all types of documents pdf,xml,doc except video and image and audio files.
And display list with name, size, date, and location.
ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
// every column, although that is huge waste, you probably need
// BaseColumns.DATA (the path) only.
String[] projection = null;
// exclude media files, they would be here also.
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_NONE;
String[] selectionArgs = null; // there is no ? in selection so null here
String sortOrder = null; // unordered
Cursor allNonMediaFiles = cr.query(uri, projection, selection, selectionArgs, sortOrder);
I am using this code for getting documents from mobile is it work? And how to get the document details like uri, date, and name. thanks advance.
Try this one:
File path;
ArrayList<String> pdf_paths=new ArrayList<String>();
ArrayList<String> pdf_names=new ArrayList<String>();
path = new File(Environment.getExternalStorageDirectory() + "");
searchFolderRecursive1(path);
private static void searchFolderRecursive1(File folder)
{
if (file.isFile())
{
//.pdf files
if(file.getName().contains(".pdf"))
{
file.getPath();
pdf_names.add(file.getName());
pdf_paths.add(file.getPath());
Log.e("pdf_paths", ""+pdf_names);
}
else if(file.getName().contains(".doc"))
{
file.getPath();
pdf_names.add(file.getName());
pdf_paths.add(file.getPath());
}
}
}
String ext =FilenameUtils.getExtension(mediSore.getString(mediSore.getColumnIndex(MediaStore.Files.FileColumns.DATA)));
if(ext.equals("pdf") || ext.equals("txt")|| ext.equals("docx") ||
ext.equals("xlsx") ||
ext.equals("csv")||
ext.equals("ods")||
ext.equals("odt")
) {
Array.add(
mediSore.getString(mediSore.getColumnIndex(MediaStore.Files.FileColumns.DATA)) + " -- " +
mediSore.getString(mediSore.getColumnIndex(MediaStore.Files.FileColumns.TITLE)) + "---" +
mediSore.getString(mediSore.getColumnIndex(MediaStore.Files.FileColumns.DISPLAY_NAME))
);
Heading

Retrieve contacts from a set of phone numbers

I wish to retrieve Contacts list based on a set of phone numbers. What is going wrong here?
Currently using this -
private String numbers = "'12345', '54321'";
String[] mSelectionArgs = {numbers};'
return new CursorLoader(getActivity(),
PhoneLookup.CONTENT_FILTER_URI,
ContactsQuery.PROJECTION,
ContactsQuery.SELECTION,
mSelectionArgs,
PhoneLookup.DISPLAY_NAME);
PROJECTION and SELECTION are defined in ContactsQuery as follows
final static String[] PROJECTION = {
PhoneLookup._ID,
PhoneLookup.LOOKUP_KEY,
PhoneLookup.DISPLAY_NAME,
PhoneLookup.PHOTO_THUMBNAIL_URI,
};
final static String SELECTION =
PhoneLookup.HAS_PHONE_NUMBER + "=1 AND " + PhoneLookup.NUMBER) + " IN (?)";
However, I get the error:
07-08 00:44:21.007: E/AndroidRuntime(2307): Caused by: java.lang.IllegalArgumentException: URI: content://com.android.contacts/phone_lookup, calling user: com.example.android.contactslist, calling package:com.example.android.contactslist
Try:
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, (DISPLAY_NAME = '" + name + "'"), null, null);

Get first and last name of a contact rather than single display name?

I am currently working with the Android Contacts content provider and currently can access a contacts full display name without issue using the following code:
String[] PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
String SELECTION = "LOWER(" + ContactsContract.Contacts.DISPLAY_NAME + ")"
+ " LIKE '" + constraint + "%' " + "and " +
ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
Cursor cur = managedQuery(ContactsContract.Contacts.CONTENT_URI,
PROJECTION, SELECTION, null, sortOrder);
However I want to be able to get both the contacts first and last name separately, I have tried to use the StructuredName in an attempt to get this but I can't seem to get it working.
Can anyone point me in the right direction as to how to use the StructuredName correctly to get the name split into First and Last?
UPDATE:
Following hovanessyan's advice I have attempted the following:
String[] PROJECTION = new String[] {
ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.HAS_PHONE_NUMBER,
};
String SELECTION = ContactsContract.CommonDataKinds.StructuredName.IN_VISIBLE_GROUP + " = '1'";
String sortOrder = ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, sortOrder);
int indexGivenName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
int indexFamilyName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
int indexDisplayName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
while (cursor.moveToNext()) {
String given = cursor.getString(indexGivenName);
String family = cursor.getString(indexFamilyName);
String display = cursor.getString(indexDisplayName);
Log.e("XXX", "Name: | " + given + " | " + family + " | " + display);
}
However using the PROJECTION causes a crash as follows:
12-08 16:52:01.575: E/AndroidRuntime(7912): Caused by: java.lang.IllegalArgumentException: Invalid column has_phone_number
If I remove the PROJECTION I get all the results printed out but a lot of them contain NULL.
For example:
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null
So can anyone see what I'm doing wrong in that my PROJECTION doesn't work?
FURTHER UPDATE:
I have sorted out the issues with my PROJECTION but now I have an issue where the DATA content provider is supplying me back with all the null data and causing NULL pointer exceptions in my code.
A cursor count from ContactsContract.Contacts gives me back 115 for example but using the DATA table gives me back 464 using the same parameters and this is causing huge issues in my app.
Anyone have any ideas why that is?
Take a look at ContactsContract.CommonDataKinds.StructuredName class. You have all the needed columns there, and you can probably do something like:
Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, other_query_params_for_filtering);
int indexGivenName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
int indexFamilyName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
int indexDisplayName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
while (cursor.moveToNext()) {
String given = cursor.getString(indexGivenName);
String family = cursor.getString(indexFamilyName);
String display = cursor.getString(indexDisplayName);
}
Here is a general function for getting user data from ContactsContract.Data table:
Map<String, String> result = new HashMap<>();
Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.CONTACT_ID + "='" + YOUR_CONTACT_ID + "'", null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String mime = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
switch (mime) {
case ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE:
result.put(FIRST_NAME, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)));
result.put(LAST_NAME, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)));
break;
case ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE:
result.put(CITY, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)));
result.put(STREET, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
result.put(ZIP, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)));
break;
case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE:
if (ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE == cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))) {
result.put(MOBILE, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
break;
}
}
cursor.close();
}
return result;
Cursor phone_cursor = cr.query(ContactsContract.CommonDataKinds.
Phone.CONTENT_URI, null, null, null, null);
while (phone_cursor.moveToNext()) {
try {
int id = Integer.parseInt(phone_cursor.getString(phone_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)));
Cursor name_cursor = cr.query(ContactsContract.Data.CONTENT_URI,null,
ContactsContract.Data.CONTACT_ID + " = " + id, null, null);
String name = phone_cursor.getString(phone_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String first_name ="";
String last_name = "";
while (name_cursor.moveToNext()) {
if(name_cursor.getString(name_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))!=null){
first_name = name_cursor.getString(name_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
last_name = name_cursor.getString(name_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
}}
name_cursor.close();
String phoneNumber = phone_cursor.getString(phone_cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
} catch (Exception e) {
}
}
phone_cursor.close();

Categories

Resources