I'd like to select an image from the gallery app installed on Android as a code.

Asked 2 years ago, Updated 2 years ago, 97 views

I want to open the gallery app's photo installed on Android from my app What can I do when I have the URI of the picture?

image android android-intent gallery

2022-09-22 22:23

1 Answers

public class BrowsePictureActivity extends Activity {

    // // this is the action code we use in our intent, 
    // // this way we know we're looking at the response from our own action
    private static final int SELECT_PICTURE = 1;

    private String selectedImagePath;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.Button01)
                .setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {
                        // Select a picture 
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent,
                                "Select Picture"), SELECT_PICTURE);
                    }
                });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
            }
        }
    }

    /**
     * Method of receiving the URI path of a picture 
     */
    public String getPath(Uri uri) {
            // Null return if uri is null 
            if( uri == null ) {
                return null;
            }
            // Gets the URI of the picture selected by the user from the media store. 
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            if( cursor != null ){
                int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            }
            // Returns the URI path. 
            return uri.getPath();
    }

}

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); In this way, allow MULTIPLE to int.

if (Intent.ACTION_SEND_MULTIPLE.equals(data.getAction()))
        && Intent.hasExtra(Intent.EXTRA_STREAM)) {
    // // retrieve a collection of selected images
    ArrayList<Parcelable> list = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
    // // iterate over these images
    if( list != null ) {
       for (Parcelable parcel : list) {
         Uri uri = (Uri) parcel;
         // // TODO handle the images one by one here
       }
   }
} 

And add this sauce to the Result part.


2022-09-22 22:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.