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();
}
Related
How I can delete image by Uri using scoped storage android 10?
I saved my images to this scoped
final String relativeLocation = Environment.DIRECTORY_PICTURES + File.separator + "myApp" + File.separator + constants.PhotoDirName;
ContentResolver resolver = context.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
I need to delete an image using Uri using this code
String[] projection;
projection = new String[]{MediaStore.MediaColumns._ID};
Uri externalUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = context.getContentResolver().query(externalUri, projection,
MediaStore.MediaColumns.DISPLAY_NAME + " LIKE ?", new String[]{displayName}, null);
assert cursor != null;
cursor.moveToFirst();
if (cursor.getCount() > 0) {
int columnIndex = cursor.getColumnIndex(projection[0]);
long fileId = cursor.getLong(columnIndex);
cursor.close();
Uri uri= Uri.parse(externalUri.toString() + "/" + fileId);
if (uri != null) {
final ContentResolver resolver = context.getContentResolver();
String[] selectionArgsPdf = new String[]{displayName};
try {
resolver.delete(uri, MediaStore.MediaColumns.DISPLAY_NAME + "=?", selectionArgsPdf);
} catch (Exception ex) {
ex.printStackTrace();
// show some alert message
}
}
}
But the image did not deleted!!
this error appears
android.app.RecoverableSecurityException: com.xxxx.xxxx has no access to content://media/external/images/media/11774
I am capturing image and storing it
Images are getting created in specified path
But I cannot display the images by obtaining from cursor
How to resolve this
How I am storing image after capturing it::
private void createFile(byte[] fileData) throws IOException {
FileOutputStream out=null;
try {
//Create the directory
String mDirectoryPath = Environment.DIRECTORY_PICTURES + LinksAndKeys.DIRECTORY_PATH_FOR_IMAGES;
String mImageName = System.currentTimeMillis()+".jpg";
File root = Environment.getExternalStoragePublicDirectory(mDirectoryPath);
File dir = new File(root + File.separator);
if (!dir.exists()) dir.mkdir();
//Create file..
File file = new File(root + File.separator + mImageName);
file.createNewFile();
out = new FileOutputStream(file);
if(out!=null){
out.write(fileData);
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
out.close();
}
}
How I am trying to get the URI's of created images::
private ArrayList<String> loadPhotosFromNativeGallery() {
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
Cursor imagecursorExternalUri = getActivity().managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy + " DESC");
Cursor imagecursorInternalUri = getActivity().managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy + " DESC");
Cursor[] cursorArray = { imagecursorExternalUri, imagecursorInternalUri};
MergeCursor mMergeCursor = new MergeCursor(cursorArray);
ArrayList<String> imageUrls = new ArrayList<String>();
for (int i = 0; i < mMergeCursor.getCount(); i++) {
mMergeCursor.moveToPosition(i);
int dataColumnIndex = mMergeCursor.getColumnIndex(MediaStore.Images.Media.DATA);
imageUrls.add(mMergeCursor.getString(dataColumnIndex));
System.out.println("=====> Array path => "+imageUrls.get(i));
}
return imageUrls;
}
The system scans the SD card when it is mounted to find any new image (and other) files. If you are programmatically adding a file, then you must scan your new file. you can use following method to do that:
MediaScannerConnection.scanFile(this, new String[] { file.getPath() }, new String[] { "image/jpg" }, null);
you can find more info from this link
you can insert your data directly with:
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "YourTitle");
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
values.put(MediaStore.MediaColumns.DATA, filePath);
context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera"
I tried this code for find the location of camera storage images but its give only "DCIM" image location.but some devices not store always in "DCIM" folder so how could i find out storage location?
Try this:
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
Try to below code work for my app perfect :-
Uri selectedImageUri = data.getData();
String selectedImagePath = getRealPathFromURI(selectedImageUri);
Method:-
public String getRealPathFromURI(Uri contentUri)
{
try
{
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
catch (Exception e)
{
return contentUri.getPath();
}
}
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);
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);
}