Android get images from sdcard - android

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);
}

Related

Getting images from gallery, some images doesn't show even image path is obtained

I'm trying to fetch all images paths inside the gallery(including subfolders) Its working fine, but some of the images are failing to show as thumbnails
*obtaining all paths inside arraylist
String[] projection = new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.DATE_TAKEN,
};
final Cursor cursor = getActivity().getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
null, null,
MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
if (cursor.moveToFirst()) {
final String imageLocation = cursor.getString(1);
locations.add(imageLocation);
while (cursor.moveToNext()) {
String tempLocation = cursor.getString(1);
boolean fileExists = new File(tempLocation).isFile();
if (fileExists) {
locations.add(tempLocation);
}
}
cursor.close();
*setting image inside ImageView
Uri uri = Uri.fromFile(new File(mItems.get(position)));
Picasso.with(getContext())
.load(uri)
.resize(200, 200)
.centerCrop()
.placeholder(R.drawable.placeholder_media)
.error(R.drawable.placeholder_error_media)
.noFade()
.into(mMediaThumb);
Use this :
Picasso.with(Context).setLoggingEnabled(true);
To check if there is an error retrieving image in logcat.

Cannot remove an image inserted with MediaStore

I'm saving an image loaded with Picasso to be shared afterwards. This is the code I'm using to save it:
String path = MediaStore.Images.Media.insertImage(
mContext.getContentResolver(), mImageBitmap, "Shared image", null);
return Uri.parse(path);
Now, when the image has been shared, I have to delete it using the URI. I've tried some answers I've seen here but none of them has worked. The image still appears in the gallery. These are the methods I have used:
// Set up the projection (we only need the ID)
String[] projection = { MediaStore.Images.Media._ID };
// Match on the file path
String selection = MediaStore.Images.Media.DATA + " = ?";
String[] selectionArgs = new String[] { new File(mImageUri.toString()).getAbsolutePath() };
// Query for the ID of the media matching the file path
Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
if (c.moveToFirst())
{
// We found the ID. Deleting the item via the content provider will also remove the file
long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
contentResolver.delete(deleteUri, null, null);
}
c.close();
File file = new File(mImageUri.toString());
// This returns false
file.delete();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(mImageUri.toString()))));
Any idea of what i've done wrong?
Thanks,
Try this for deletion
String[] retCol = { MediaStore.Audio.Media._ID };
Cursor cur = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
retCol,
MediaStore.Images.Media.TITLE + "='"+TEMP_FILE_TITLE+"'", null, null
);
try {
if (cur.getCount() == 0) {
return;
}
cur.moveToFirst();
int id = cur.getInt(cur.getColumnIndex(MediaStore.MediaColumns._ID));
Uri uri = ContentUris.withAppendedId(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id
);
int cnt = context.getContentResolver().delete(uri, null, null);
}finally {
cur.close();
}
In your case replace TEMP_FILE_TITLE = "Shared image"

Unable to get images from internal memory in Android?

File file = new File(Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/Pics/");
if (!file.exists()) {
file.mkdirs();
}
File[] pictures = file.listFiles();
file exists returns true but pictures returns null
Use a ContentResolver to find the images.
Uri photo = data.getData();
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor cursor = getActivity().getContentResolver().query(photo, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
textureName = cursor.getString(column_index);
try {
Bitmap bitmap = BitmapFactory.decodeFile(textureName);
} catch (IOException ioEx) {
ioEx.printStackTrace();
}

get pictures's filepath taken by camera

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);

How to get the thumbnail from .mov files in Android?

I using the following code to get the thumbnail of JPG and AVI file , but I can not get the thumbnail from .mov files.
String[] projection_image = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, };
String[] projection_video = { MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID, };
File file = new File(viewTag.mFileNode.mName) ;
String tempfilePath = file.getPath();
String whereClause_image = MediaStore.Images.Media.DATA + " = '" + tempfilePath + "'";
String whereClause_video = MediaStore.Video.Media.DATA + " = '" + tempfilePath + "'";
ContentResolver cr = getActivity().getContentResolver();
Cursor image_cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection_image, whereClause_image, null, null);
Cursor video_cursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection_video, whereClause_video, null, null);
try {
if(image_cursor.moveToFirst()){
long thumID = image_cursor.getLong(image_cursor.getColumnIndex("_ID"));
Bitmap bitmap_image = MediaStore.Images.Thumbnails.getThumbnail(getActivity().getContentResolver(), thumID, Images.Thumbnails.MICRO_KIND, null);
viewTag.mThumbnail.setImageBitmap(bitmap_image) ;
}
if(video_cursor.moveToFirst()){
long thumID = video_cursor.getLong(video_cursor.getColumnIndex("_ID"));
Bitmap bitmap_video = MediaStore.Video.Thumbnails.getThumbnail(getActivity().getContentResolver(), thumID, Video.Thumbnails.MICRO_KIND, null);
viewTag.mThumbnail.setImageBitmap(bitmap_video) ;
}
} catch (NullPointerException e) {
// TODO: handle exception
Log.i(TAG, "cursor---NullPointerException");
image_cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection_image, whereClause_image, null, null);
video_cursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection_video, whereClause_video, null, null);
} finally{
image_cursor.close();
video_cursor.close();
}
Does somebody know how to get the thumbnail from .mov files by using MediaStore in Android ?
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path,
MediaStore.Images.Thumbnails.MINI_KIND);

Categories

Resources