get pictures's filepath taken by camera - android

I want to get the pictures which were taken by only device camera.
Maybe, filepath name have DCIM or other things.
Here is code, is this ok? target API is bitween 15~21API.
/**
* #param context
* #returnArrayList with images Path
*/
public static ArrayList<String> getAllShownImagesPath(Context context) {
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name;
ArrayList<String> listOfAllImages = new ArrayList<String>();
String absolutePathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
final String DCIMPath = android.os.Environment.DIRECTORY_DCIM;
Log.d(TAG, "DCIMPath #"+DCIMPath);
String[] projection = { MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
cursor = context.getContentResolver().query(uri, projection, null, null, null);
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);
if(absolutePathOfImage.contains(DCIMPath))
listOfAllImages.add(absolutePathOfImage);
}
String filepath = Environment.getExternalStorageDirectory().toString();
Log.d(TAG, "filepath #" + filepath);
return listOfAllImages;
}
I only tests one device it worked. but android device is so many.

If you want to get the default pictures storage directory on your device, try :
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

Related

How query images/videos in custom folders using ContentResolver in Android?

I have a custom folder in the Pictures directory, like thisPictures/MyFolder. It has images in MyFolder. Here is how to query the images using ContentResolver on MyFolder folder only.
I tried this
Cursor mediaCursor = context.getContentResolver().query(
MediaStore.Files.getContentUri("external"),
null,
MediaStore.MediaColumns.RELATIVE_PATH + " like ? ",
new String[]{"%MyFolder%"},
null);
But it contains other files also. Or is any alternate to content resolver?
You can use the below function to get the images from the folder.
private void getImageFolderList() {
String[] projection = new String[] { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN };
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
Cursor cur = getContentResolver().query(images, projection, // Which
// columns
// to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
orderBy + " DESC" // Ordering
);
ArrayList<String> imagePath;
if (cur.moveToFirst()) {
String bucket, date;
int bucketColumn = cur.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
int dateColumn = cur.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);
do {
bucket = cur.getString(bucketColumn);
date = cur.getString(dateColumn);
if (!allFolder.contains(bucket)) {
allFolder.add(bucket);
}
imagePath = listImageByFolder.get(bucket);
if (imagePath == null) {
imagePath = new ArrayList<String>();
}
imagePath.add(cur.getString(cur
.getColumnIndex(MediaStore.Images.Media.DATA)));
listImageByFolder.put(bucket, imagePath);
} while (cur.moveToNext());
}
}

How can I get Uri of a private Folder in Android?

I am trying to make an android camera app with image gallery. The images captured are saved to a private directory: Android/data/com.example.newcamera/files/pictures.
Whenever I am using INTERNAL_CONTENT_URI or, EXTERNAL_CONTENT_URI as Uri, The app is bringing all the public pictures of my phone but not the one in the private directory. But I need only those with private directory. How can I get it? Please help me. My code snippet is as follows:
Thanks in advance.
protected String doInBackground(String... args) {
String xml = "";
String path = null;
String album = null;
String timestamp = null;
String countPhoto = null;
Uri uriInternal = MediaStore.Images.Media.INTERNAL_CONTENT_URI;
Uri uriExternal = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Uri myUri = Uri.fromFile(new File(getApplicationContext().getFilesDir().getAbsolutePath()));
String[] projection = { MediaStore.MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.MediaColumns.DATE_MODIFIED };
Cursor cursorExternal = getContentResolver().query(uriExternal, projection, "_data IS NOT NULL) GROUP BY (bucket_display_name",
null, null);
Cursor cursorInternal = getContentResolver().query(uriInternal, projection, "_data IS NOT NULL) GROUP BY (bucket_display_name",
null, null);
Cursor myCursor = getContentResolver().query(myUri, projection, "_data IS NOT NULL) GROUP BY (bucket_display_name",
null, null);
Cursor cursor = new MergeCursor(new Cursor[]{cursorExternal, cursorInternal, myCursor});
while (cursor.moveToNext()) {
path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
timestamp = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_MODIFIED));
countPhoto = Function.getCount(getApplicationContext(), album);
albumList.add(Function.mappingInbox(album, path, timestamp, Function.converToTime(timestamp), countPhoto));
}
cursor.close();
Collections.sort(albumList, new MapComparator(Function.KEY_TIMESTAMP, "dsc")); // Arranging photo album by timestamp decending
return xml;
}
You can fetch your files from particular folder by:
File folder = new File(Environment.getExternalStorageDirectory().toString() + "/Folder Name/");
folder.mkdirs();
File[] allFiles = folder.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return (name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".png"));
}
});
You can convert file path to Uri by Uri.fromFile(YOUR FILE)

Content provider not working in one plus devices

I have two applications, one contains content provider and other app receives data using content resolver. If i add any data form provider that should be displayed from receiver in second app, this is the expected functionality.But after adding data once I remove first app from stack then second app displays null cursor,If I keep first app in stack, then second app displays correct value .(This issue only comes in one plus devices)
code snippet where cursor value coming null is,
Cursor c = getContentResolver().query(CONTENT_URI, null, null, null,
null);
may be it will help you
private String uriToFilename(Uri uri) {
String path = null;
if (Build.VERSION.SDK_INT < 11) {
path = getRealPathFromURI_BelowAPI11(this, uri);
} else if (Build.VERSION.SDK_INT < 19) {
path = getRealPathFromURI_API11to18(this, uri);
} else {
path = getRealPathFromURI_API19(this, uri);
}
return path;
}
BelowAPI11
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor 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);
}
API11to18
public static String 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();
if (cursor != null) {
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
API19
public static String getRealPathFromURI_API19(Context context, Uri uri) {
Log.e("uri", uri.getPath());
String filePath = "";
if (DocumentsContract.isDocumentUri(context, uri)) {
String wholeID = DocumentsContract.getDocumentId(uri);
Log.e("wholeID", wholeID);
// Split at colon, use second item in the array
String[] splits = wholeID.split(":");
if (splits.length == 2) {
String id = splits[1];
String[] column = {MediaStore.Images.Media.DATA};
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
}
} else {
filePath = uri.getPath();
}
return filePath;
}
Enable Don’t optimize inside Settings to resolve one plus issue.
Settings –> Battery –> Battery Optimization –> Your App –> Don’t optimize
enter image description here

How to get file path from uri?

I am trying to retrieve file path from URI. But my cursor is returning null.
Two problems:
Uri may be audio/ video. So how can I retrieve file path.
Why cursor is returning null?
Here is my code
String[] proj = { MediaStore.Video.Media.DATA };
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
//here cursor is null. So i am getting Null pointer exception
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
Permissions I have used:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
public void songList(){
ContentResolver contentResolver = getContentResolver();
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cur = contentResolver.query(uri, null, null, null, null);
if(cur.moveToFirst()){
do {
int pathIndex = cur.getColumnIndex(MediaStore.Audio.Media.DATA);
int nameIndex = cur.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME);
String spath = cur.getString(pathIndex);
String name = cur.getString(nameIndex);
paths.add(spath.substring(4));
songs.add(name);
} while (cur.moveToNext());
}
You can use get File path from diffrent SDk versions
Use RealPathUtils for it
public class RealPathUtils {
#SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
#SuppressLint("NewApi")
public static String 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();
if(cursor != null){
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){
String[] proj = { MediaStore.Images.Media.DATA };
Cursor 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);
}
}
Now get the file Path from URI
String path = null;
if (Build.VERSION.SDK_INT < 11)
path = RealPathUtils.getRealPathFromURI_BelowAPI11(MainActivity.this, uri);
// SDK >= 11 && SDK < 19
else if (Build.VERSION.SDK_INT < 19)
path = RealPathUtils.getRealPathFromURI_API11to18(MainActivity.this, uri);
// SDK > 19 (Android 4.4)
else
path = RealPathUtils.getRealPathFromURI_API19(MainActivity.this, uri);
Log.d(TAG, "File Path: " + path);
// Get the file instance
File file = new File(path);

Android get images from sdcard

I am using a code which is listing all the images from my device...and I'm trying to figure it out how to get images only from a specific folder, not all the images. Here is the code I'm using :
ArrayList<Bitmap> images = new ArrayList<Bitmap>();
String[] projection = {MediaStore.Images.Thumbnails.DATA};
Uri uri = Uri.parse("content://media/external/images/media");
cursor = managedQuery( uri, projection, null, null, null);
//cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
Log.i("MediaStore.Images.Media.EXTERNAL_CONTENT_URI", "MediaStore.Images.Media.EXTERNAL_CONTENT_URI: " + MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if(cursor.getCount()==0){
Log.i("No Cards","No Cards");
cursor.close();
} else if(cursor.getCount()>0){
for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()){
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String imagePath = cursor.getString(columnIndex);
Log.i("imagePath", "imagePath: " + imagePath);
Bitmap b = BitmapFactory.decodeFile("/Stampii" + imagePath, null);
images.add(b);
}
}
The thing that I want to do is to get images from imagePath: /mnt/sdcard/Stampii/MediaCategory-251.jpg Stampii folder, but I can't understand how to enter the right path to that folder. I've already tried with :
Uri uri = Uri.parse("content://media/external/images/media/mnt/sdcard/Stampii");
Any solutions?
use
File file = new File(Environment.getExternalStoragePath()+"/Stampii/");
file imageList[] = file.listFiles();
for(int i=0;i<imageList.length;i++)
{
Log.e("Image: "+i+": path", imageList[i].getAbsolutePath());
Bitmap b = BitmapFactory.decodeFile(imageList[i].getAbsolutePath());
images.add(b);
}

Categories

Resources