'How to find and retrieve live photos from external or media storage in android?

I want to retrieve all live photos from android device, any advices?



Solution 1:[1]

You can try this :

check if these permissions exists in your manifest :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Call this method to get all photos from the gallery :

public static ArrayList<Photo> getAllImages(Cursor cursor, Activity activity) {
       int i = 0;
       ArrayList<Photo> arrayList = new ArrayList<>();
       if (cursor == null) {
           ContentResolver resolver = activity.getContentResolver();
           cursor = resolver.query(
                   MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                   null,
                   null,
                   null,
                   null);
           if (cursor != null) {
               while (i < cursor.getCount()) {
                   cursor.moveToPosition(i);
                   int fieldIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);
                   Long id = cursor.getLong(fieldIndex);
                   Uri imageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
                   Photo photo = new Photo();
                   photo.setName("");
                   photo.setUri(imageUri);
                   arrayList.add(photo);
                   i++;
               }
               cursor.close();
           }

       }
       return arrayList;
   }

Create a class and name it Photo :

public class Photo {
    private String name;
    private Uri uri;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Uri getUri() {
        return uri;
    }

    public Photo(String name, Uri uri) {
        this.name = name;
        this.uri = uri;
    }

    public Photo() {
    }

    public void setUri(Uri uri) {
        this.uri = uri;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mouaad Abdelghafour AITALI