Get all photos from Android device android programming

前端 未结 2 1301
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-19 09:21

I am trying to fetch all the photos of my android device.

I have an onCreate function:

public void onCreate(Bundle savedInstanceState)          


        
相关标签:
2条回答
  • 2021-01-19 09:32

    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;
    }
    
    0 讨论(0)
  • 2021-01-19 09:59

    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;
    
    0 讨论(0)
提交回复
热议问题