-1

I am making an android album app where I can create an album and add photos and delete photos from the album. Adding a photo is a bit tricky where I need a photo filename with a file path. This was very easy using JFileChooser in java but this is android and I have no clue on getting the filename and file path. Is there any thing in the android api where I can get the same functionality as the JFileChooser.

I am looking for a solution to this problem either using a file chooser of some sort or an entire to new approach. Any help is appreciated..

Or is there any other approach I can implement to add a photo...

cylon
  • 735
  • 1
  • 11
  • 26
jrdnsingh89
  • 215
  • 2
  • 7
  • 18
  • [Here](http://ihhira.com/blog/2013/10/30/android-file-chooser-dialog/) is a file chooser. It is kind of java FileChooser replacement. – imranhasanhira Oct 29 '13 at 20:47

1 Answers1

2

You may use Intent.ACTION_PICK to invoke an image picker. This intent may be caught by the default gallery app, or some other app installed on the device.

private static final int REQUEST_PICKER = 1;

private void invokePicker() {
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/*");
    startActivityForResult(Intent.createChooser(intent, "Complete action using"), REQUEST_PICKER);
}

Then receive the result on onActivityResult.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK)
        return;
    if (requestCode == PICK_FROM_FILE) {
        // Get Uri of the file selected,
        Uri theImageUri = data.getData();
        // Or if you want a Bitmap,
        Bitmap theBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), theImageUri);
    }
}

Edited:

Though in this way you don't need a real file path, you can get it from MediaStore if you need.

Zain Aftab
  • 703
  • 7
  • 21
tnj
  • 921
  • 7
  • 12
  • So this will give me a window where I can choose one photo to add to my album? can you post a screen shot of this working of a button. I know button has onclicklistener then I can add the above code and it will work? – jrdnsingh89 Apr 17 '13 at 01:24
  • Yes, it's fairly simple so just try it, call `invokePicker()` above in your button click handler to see what will be shown. :) Sorry, I edited my answer to correct some mistakes so be sure to reload the page before copying it. – tnj Apr 17 '13 at 01:41