Open Folder specifed in edittext android - android

Am working on showing images from gallery BELOW IS THE CODE.
HERE i want to open images in a specific folder which name will be given in a edit text.
Please help me in doing it as currently its opening all the images which is irrelevant to the application and hard for me to identify the images.
protected void LoadGalleryImages() {
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
int image_column_index = 0;
if (imagecursor != null) {
image_column_index = imagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
count = imagecursor.getCount();
}
imgSelected = new String[count];
arrPath = new String[count];
thumbnailsselection = new boolean[count];
for (int i = 0; i < count; i++) {
if (imagecursor != null) {
imagecursor.moveToPosition(i);
// int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
}
}

Pooja,
Here is the code to open your specified folder to browse your files:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse("folder path");
//Change content type as you want if you dont know then just mention "*/*"
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
Hope this will help you.

Related

MediaStore.Images.Media is not capable of showing all images, solution

doing a query with MediaStore.Images.Media does not show all images whether it is querying internal content or external content
The results (the thumbnails and the location those thumbnails are retrieved from) are not the same across even mainstream devices. Devices with no SD card and only internal storage have different image results, Samsung devices have different kinds of results, Google devices have different kinds of results.
Specifically with this code that is used around stackoverflow as a solution,
final String[] columns = {MediaStore.Images.Thumbnails._ID};
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, orderBy);
if (imagecursor != null) {
int image_column_index = imagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
int count = imagecursor.getCount();
for (int i = 0; i < count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
ImageItem imageItem = new ImageItem();
imageItem.id = id;
lastId = id;
imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
context.getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MINI_KIND, null);
images.add(imageItem);
}
imagecursor.close();
I look at the code and it seems sound, but I need a better solution because results vary, and on some devices I have no idea where the resulting thumbnails have been pulled from
As #danny117 pointed out to me, trying to get THUMBNAILS was a flawed assumption. One cannot rely on the existence of thumbnails for every image in the android system.
So I ultimately retrieve all images based on mime type, and use the Files MediaStore
String[] projection = {
MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns.MEDIA_TYPE,
MediaStore.Files.FileColumns.MIME_TYPE,
MediaStore.Files.FileColumns.TITLE
};
// Return only video and image metadata.
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
+ " OR "
+ MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;
Uri queryUri = MediaStore.Files.getContentUri("external");
CursorLoader cursorLoader = new CursorLoader(
getActivity(),
queryUri,
projection,
selection,
null, // Selection args (none).
MediaStore.Files.FileColumns.DATE_ADDED + " DESC" // Sort order.
);
images.clear();
/*
final String[] columns = {MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATE_ADDED};
final String orderBy = MediaStore.Images.Media.DATE_ADDED + " DESC";
Cursor imagecursor = getActivity().getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, orderBy);
*/
Cursor imagecursor = cursorLoader.loadInBackground();
if (imagecursor != null) {
int image_column_index = imagecursor
.getColumnIndex(MediaStore.Files.FileColumns._ID);
int type_column_index = imagecursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE);
int count = imagecursor.getCount();
for (int i = 0; i < count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
String mime_type = imagecursor.getString(type_column_index);
ImageItem imageItem = new ImageItem();
imageItem.id = id;
//lastId = id;
if(!mime_type.contains("video"))
imageItem.uriString = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(id)).toString();
else
imageItem.uriString = MediaStore.Video.Media.EXTERNAL_CONTENT_URI.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, String.valueOf(id)).toString();
images.add(imageItem);
}
//add ImageItem at top of list
imagecursor.close();
}

Get all photos from Android device android programming

I am trying to fetch all the photos of my android device.
I have an onCreate function:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageAdapter adapter= new ImageAdapter(this);
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
Uri rr= MediaStore.Images.Media.INTERNAL_CONTENT_URI;
Cursor imagecursor = getContentResolver().query(
MediaStore.Images.Media.INTERNAL_CONTENT_URI,null, null,
null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
adapter.mThumbIds[i]= thumbnails[i];
}
}
where the count returned on imageCursor.getCount() is returning 0.
Can someone please guide me since I am not able to fetch any media using this code?
Note: I am testing on Galaxy Note-2
First do not forget to
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
This method will return list of photo in your gallery
public static ArrayList<String> getImagesPath(Activity activity) {
Uri uri;
ArrayList<String> listOfAllImages = new ArrayList<String>();
Cursor cursor;
int column_index_data, column_index_folder_name;
String PathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
cursor = activity.getContentResolver().query(uri, projection, null,
null, null);
column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
column_index_folder_name = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
PathOfImage = cursor.getString(column_index_data);
listOfAllImages.add(PathOfImage);
}
return listOfAllImages;
}
just give it a try even if your device has only internal storage no external storage,
try adding permissions:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and read using
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

Get Listing Of All Photos (Even pictures stored online) - Picasa image database Uri?

Anyone know how to get a listing of all images that show up in the Android Gallery?
This query is only gets pictures taken locally on the phone. Does anyone know the URI where the Picasa image database is stored?? Appreciate the help.
private void getListOfAllPictures()
{
final String[] filePathColumn = { MediaStore.Images.Media._ID, MediaColumns.DATA, MediaColumns.DISPLAY_NAME, Images.Media.ORIENTATION, Images.Media.LATITUDE, Images.Media.LONGITUDE };
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
filePathColumn, null, null, null);
Vector<ImageDescriptor> imageDescriptors = new Vector<ImageDescriptor>();
if(cursor != null)
{
cursor.moveToFirst();
int IMG_ID_INDEX = cursor.getColumnIndex(MediaStore.Images.Media._ID);
int DATA_INDEX = cursor.getColumnIndex(MediaColumns.DATA);
int LATITUDE_INDEX = cursor.getColumnIndex(Images.Media.LATITUDE);
int LONGITUDE_INDEX = cursor.getColumnIndex(Images.Media.LONGITUDE);
int ORIENTATION_INDEX = cursor.getColumnIndex(Images.Media.ORIENTATION);
while(!cursor.isAfterLast())
{
//Blah Blah
cursor.moveToNext();
}
}
cursor.close();
Log.v(TAG, "Found " + imageDescriptors.size() + " images.");
}
ok its my code and it work for me it give all images which i can see in Android Gallery
just call this function from this line
getallimages(Environment.getExternalStorageDirectory());
and my function is below
private void getallimages(File dir)
{
String[] STAR = { "*" };
final String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER;
Cursor imagecursor = cntx.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STAR, null, null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
int count = imagecursor.getCount();
for (int i = 0; i < count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
ImageItem imageItem = new ImageItem();
imageItem.filePath = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA));
imageItem.id = id;
imageItem.selection = false; //newly added item will be selected by default
controller.images.add(imageItem);
}
}

Select a folder if images size is less than 1mb android

I am opening a folder onClick and sending images thro FTP. But when the size of image is more than 2MB i face an error in transmission. So I want open the folder if it has images with size less than 1MB else it will toast a message to check. Is this scenario possible as am new to android please help me.
protected void LoadGalleryImages() {
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
String foldername=sharedpre.getString("FolderName", "");
imagecursor=managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%/"+foldername+"/%"},null);
int image_column_index = 0;
if (imagecursor != null) {
image_column_index = imagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
count = imagecursor.getCount();
}
imgSelected = new String[count];
arrPath = new String[count];
thumbnailsselection = new boolean[count];
for (int i = 0; i < count; i++) {
if (imagecursor != null) {
imagecursor.moveToPosition(i);
int dataColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
you can use MediaStore.Images.Media.SIZE in your managequery and filter it for particular size, so can get only those images which you required according to your requirement..
EDIT :
protected void LoadGalleryImages() {
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
String foldername=sharedpre.getString("FolderName", "");
imagecursor=managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%/"+foldername+"/%"},null);
int image_column_index = 0;
if (imagecursor != null) {
String[] sizecolumns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.SIZE };
Cursor imgcursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, sizecolumns,MediaStore.Images.Media.SIZE + " < ? ", new String[] {"1024"},null);
if(imgcursor !=null) {
// Your Code
}
}
imgSelected = new String[count];
arrPath = new String[count];
thumbnailsselection = new boolean[count];
for (int i = 0; i < count; i++) {
if (imagecursor != null) {
imagecursor.moveToPosition(i);
int dataColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
With
File [] files = %FOLDER%.listFiles();
you get the files of a directory. %FOLDER% is your directory you want to check. Just iterate over these files and return false if one of them is bigger than one MB.
if(files.length > 1048576){
return false;
}
Put it in a method, pass the folder as argument and that is it.

The image's information and thumbnail are stored in different database?

I use the following code to get thumbnail and fill them in gridView.
I guess that the image's information such as BUCKET_DISPLAY_NAME,MediaStore.Images.Media.DATA and MediaStore.Images.Media._ID are stored in a database,
and the thumbnail of the image are stored in another database.
So if I want to delete a image, I need to delete three parts, the one is physical file, the two is the record of the image's information in a database, and the three is the recodr of thumbnail in another database, right?
The following code is only to delete two parts, the one is physical file, the two is the record of the image's information in a database. How can I delete the record of thumbnail? Thanks!
BTW, the code
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
is not very good, because it's asynchronous, I need to get the latest thumbnails at once after I delete a image.
And more, will
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())))
update both the image's information database and thumbnails database ?
private void FillImageAndThumb() {
this.count = curPublic.getCount();
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
this.thumbnailsselection = new boolean[this.count];
this.myID=new int[this.count];
for (int i = 0; i < this.count; i++) {
curPublic.moveToPosition(i);
int id = curPublic.getInt(curPublic.getColumnIndex(MediaStore.Images.Media._ID));
int dataColumnIndex =curPublic.getColumnIndex(MediaStore.Images.Media.DATA);
myID[i]=id;
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(),
id,
MediaStore.Images.Thumbnails.MICRO_KIND,
null);
arrPath[i] = curPublic.getString(dataColumnIndex);
}
}
private void SetCurPublic(AdapterView<?> arg0,int index){
String[] projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATA
};
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if (index==0){
curPublic = getContentResolver().query(
images,
projection,
"",
null,
""
);
}else{
String s=arg0.getSelectedItem().toString();
curPublic= getContentResolver().query(
images,
projection,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME+"=?",
new String[]{s},
"" // Ordering
);
}
}
public void DeleteSelected() {
for (int i = 0; i < thumbnailsselection.length; i++) {
if (thumbnailsselection[i]) {
File fdelete = new File(arrPath[i]);
if (fdelete.exists()) {
if (fdelete.delete()) {
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String mSelectionClause = MediaStore.Images.Media._ID
+ "=?";
String[] mSelectionArgs = { String.valueOf(myID[i]) };
getContentResolver().delete(
images,
mSelectionClause,
mSelectionArgs
);
}
}
}
}
}

Categories

Resources