How to get last image captured with camera in android? [duplicate] - android

This question already has answers here:
get the last picture taken by user
(3 answers)
Closed 9 years ago.
My intention is not to take a picture and then save it to sd card , get the link and all. The image is already taked with the original camera app in the android.
All i need it how can i get that image path with respect to sd card like
emulated/0/sdcard/DCIM/100ANDRO/image.jpg
how do i get that format of the recently taken image.

I've accomplished it like this:
final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
imageCursor.moveToFirst();
do {
String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
if (fullPath.contains("DCIM")) {
//--last image from camera --
return;
}
}
while (imageCursor.moveToNext());

Use this to get path of file from URI:
Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURI(selectedImageUri);
public 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();
}
}
}
Cursor provides random read-write access to the result set returned by a database query.
getContentResolver () returns a ContentResolver instance for your application's package.
When you want to access data in a content provider, you use the ContentResolver object in your application's Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results.
The Content Resolver includes the CRUD (create, read, update, delete) methods corresponding to the abstract methods (insert, delete, query, update) in the Content Provider class. The Content Resolver does not know the implementation of the Content Providers it is interacting with (nor does it need to know); each method is passed an URI that specifies the Content Provider to interact with.
MediaStore: The Media provider contains meta data for all available media on both internal and external storage devices. MediaStore.images contains meta data for all available images.

Related

How to list all pdf files on android 10?

Since the changes related to the authorizations of access to the shared storage, it does not seem any more possible to search all the documents of the type pdf by this approach (with requestLegacyExternalStorage = "false"):
ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
String[] projection = null;
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_NONE;
String[] selectionArgs = null;
String sortOrder = null;
Cursor allNonMediaFiles = cr.query(uri, projection, selection, selectionArgs, sortOrder);
Check this link : Media data restrictions
The only solution I see is to scan in a recurcive way all the tree of the shared storage with SAF, which seems to me very expensive in resources and ridiculous.
Does anyone have another idea?
The basic idea of scoped storage is exactly to avoid this kind of behavior, you can't know if there are or not some files somewhere in the user phone. You can just ask for permission to access to the storage tree and scan everything as you said. Even in this case the user could select a folder different from the root so your app will be limited to that folder. The idea could be perform a scan and then update your database keeping in sync using a job (job service) scheduled on the modification of tree URI and descendants.
if your project targeted to Sdk level 29 you should add to your AndroidManifest.xml the flag android:requestLegacyExternalStorage="true" under your <application> tag.
public List<String> queryFilesFromDevice(Uri uri, String[] projection, String selection, final Context context) {
final List<String> tempList = new ArrayList<>();
Cursor c = context.getContentResolver().query(uri, projection,
selection,
null,
null);
if (c != null) {
while (c.moveToNext()) {
String path = c.getString(0);
long size = c.getLong(1);
// your code logic should be here
}
c.close();
}
tempList.add(path);
return tempList;
}
you need to call the function like this:
String pdfExt = "_data LIKE '%.pdf'";
Uri ducumentsUri = MediaStore.Files.getContentUri("external");
String[] docsProjection ={MediaStore.Files.FileColumns.DATA,MediaStore.Images.Media.SIZE,MediaStore.Files.FileColumns.MIME_TYPE,};
queryFilesFromDevice(ducumentsUri, docsProjection, pdfExt, this);

Get filename and path from URI from mediastore in Nought and Oreo device

I have image uri like this :"content://com.iceburgapp.provider/external_files/.pkgName/File1.jpg"
Now whenever I get path it give me : "/external_files/.pkgName/File1.jpg"
I want to get RealPathFrom content Uri.
I got solution from stackoverflow and I tried below code but not working for me :
public 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();
}
}
}
AnyOne know How to do this? I got below error for using above code.
java.lang.IllegalArgumentException: column '_data' does not exist. Available columns: []
It's only not working in the Nought and oreo device because of contentUri using file provider.
Use a ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri. Ideally, just use that stream directly, for whatever it is that you are trying to do. Or, use that InputStream and some FileOutputStream on a file that you control to make a copy of the content, then use that file.
This is working properly in devices before Android N

Convert content:// uri into a file path

I am having problems converting a uri to a path because of the content tag. So I am trying to select any file type from the Android storage, and I am able to select a file but when I grab the data and try to convert it to a string path my app crashes.
My code to convert the uri looks like
String path = data.getData().getPath();
I've looked around and some say to use a Content provider and content resolver, but I'm not sure how to use them. Any help would be great thanks.
Display it/ upload it to an s3 bucket. When I mean display it, I mean if it's a photo, or video to show it and if it's an audio file I'd like to be able to play it and the same with other files, like PDF and so on.
This method returns the Path as String
private String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null,null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

How to obtain Cursor via ContentResolver on ANY device?

So far, we have solved Cursor == null when retrieved via ContentResolver by using separate logic for SDKs <11, 11-18, >=19. Something like
public static Cursor getRealPathFromURI_API19(Context context, Uri uri) {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
return cursor;
}
public static Cursor getRealPathFromURI_API11to18(Context context, Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
String result = null;
CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
return cursor;
}
public static Cursor getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
return cursor;
}
However, on Galaxy S5, when we save an image to its own directory in the internal public memory (not private data/data), we get cursor==null. On other devices, we DON't get null cursor.
The flow of the app is like this:
Take image with Camera
Save it into file in its own directory (public directory in internal memory)
Access file via ContentResolver and return Cursor
Steps 1 and 2 are properly done. Verified! I can see the image I take via Camera inside the specified directory.
I also checked if the Bitmap is accessible via
InputStream input;
Bitmap bmp;
try {
input = getContentResolver().openInputStream(uri);
bmp = BitmapFactory.decodeStream(input);
} catch (FileNotFoundException e1) {
Log.e("tafg", "error");
}
and I never get an exception.
However, in spite of all this, Cursor remains null on some devices. Anyone can guess why and what is the ultimate way never to get null cursor?
PS. is there any 3rd party library that handles this part properly?
PPS. We are using Retrofit to get up to 10 images saved via Camera and upload to the remote server. Using and working with Bitmap is not possible in this case as we get OOM on the 5th or 6th image. So Retrofit logic must use ContentResolver to get hold of the image(s) that need(s) to be uploaded.
So far, we have solved Cursor == null when retrieved via ContentResolver by using separate logic for SDKs <11, 11-18, >=19.
I strongly recommend that you delete all that code and use a Uri properly.
The flow of the app is like this:
Your step #3 is pointless. You know where the file is, because you put it there in step #2. And, by getting rid of step #3, you can also get rid of all of the bogus "real path" code.
Anyone can guess why
Perhaps MediaStore does not know about the image, because it has not been indexed yet. See MediaScannerConnection and its scanFile() method.
Also note that you are not actually setting cursor to a value in getRealPathFromURI_API19(). I would expect that code to not compile, so I am assuming that it is a copy/paste problem in your question.
what is the ultimate way never to get null cursor?
Stop querying for it in the first place.

Extract a contact's photo

Gah, another scenario here something that should be simple is proving to be very time-consuming and painful.
I'm using this to query the contacts provider:
private Cursor getContacts(){
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID
};
......
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
This works fine and retrieves contact names, and on a handful of contacts it shows a numeric ID for the PHOTO_ID field, which I assume is the PHOTO_ID I'm requesting. But then I push that ID into this method to extract the bitmap, it fails on every contact and the stream is null every time. I'm testing against a set of contacts that includes some with Android contact photos (I know there are some issues extracting photos from Facebook contacts).
private Bitmap loadContactPhoto(long id) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri);
if (input == null) return null;
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
}
What have I missed?
openContactPhotoInputStream() takes the uri of the contact, try calling it with the ContactsContract.Contacts._ID column instead of the PHOTO_ID column and you should see better results.
There's a bunch of relevant discussion here with some code to check out:
How do I load a contact Photo?
Note that in some cases you'll see a photo in the native contacts app which won't load through the content resolver. Some sync info, like Facebook for example, is flagged to be used only by the contacts app itself and doesn't get exported to other apps :-(
However, using the contactUri should take care of at least some of your issues.

Categories

Resources