Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 226
Nishu Avatar asked Sep 21 '14 09:09

Nishu


1 Answers

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;
}
like image 65
Moien.Dev Avatar answered Oct 23 '22 22:10

Moien.Dev