RSS

Fetch images from device.

18 Jan

Fetch all the images present on the internal as well as external storage.

public void getAllImagesPathFromDevice(){
		try {
			//Create new txt file on sdcard to store images path
			File file = new File("sdcard/GalaryImageInfo.txt");
			if(!file.exists()){
				file.createNewFile();
			}

			//Find images present on external storage
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
			String[] projection = { MediaStore.Images.Media.DATA };
			Cursor cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
			bufferedWriter.write("External memory images info is\n");
			bufferedWriter.write("-------------------------------\n");
			int counterExternal = 1;
			if(cursor.moveToFirst()){
				do{
					int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
					String filePath = cursor.getString(column_index);
					//Dump image path to txt file one by one
					bufferedWriter.write("Image Position: "+counterExternal+", Image Path: "+filePath+"\n");
					counterExternal ++;
				}while(cursor.moveToNext());
			}
			bufferedWriter.write("*********************************\n\n");

			//Find images present on internal storage
			Cursor internalCursor = managedQuery(MediaStore.Images.Media.INTERNAL_CONTENT_URI, projection, null, null, null);
			bufferedWriter.write("Internal memory images info is\n");
			bufferedWriter.write("-------------------------------\n");
			int counterInternal = 1;
			if(internalCursor.moveToFirst()){
				do{
					int column_index = internalCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
					String filePath = internalCursor.getString(column_index);
					//Dump image path to txt file one by one
					bufferedWriter.write("Image Position: "+counterInternal+", Image Path: "+filePath+"\n");
					counterInternal ++;
				}while(internalCursor.moveToNext());
			}

			bufferedWriter.close();
			cursor.close();
			internalCursor.close();
		}
		catch (Exception e) {
			e.toString();
		}
	}


Output:

Image Position: 160, Image Path: /mnt/sdcard/DCIM/Camera/2013-01-18 16.23.16.jpg
Image Position: 161, Image Path: /mnt/sdcard/DCIM/Camera/2013-01-18 16.27.42.jpg

Advertisements
 
Leave a comment

Posted by on January 18, 2013 in Android Source Code

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: