AHSAN KHAN
AHSAN KHAN

Reputation: 436

Android development Image Gallery path

I am working on app which should display all images from my sd card. it is gallery app but i cant make it to do it little weak in android it only get images from my mention path if i dont mention any path no images just root dir

String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
 .getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/dir"; 
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files){
myImageAdapter.add(file.getAbsolutePath());

Upvotes: 1

Views: 2793

Answers (3)

Jorge Casariego
Jorge Casariego

Reputation: 22212

You can use this method to get the default Gallery Path

private static String getDefaultGalleryPath() {
    return Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/";
}

What is DIRECTORY_DCIM?

The traditional location for pictures and videos when mounting the device as a camera.

Upvotes: 0

rickyalbert
rickyalbert

Reputation: 2652

With this two lines of code you will pick the images stored into the default gallery directory:

File picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File[] listFiles = picturesDirectory.listFiles();

If you need to search into ALL sdcard use this recursive method:

public static void allFiles(File root, List<File> list){
   for (File node : root.listFiles())
   {
       if (node.isDirectory())
           allFiles(node, list);
       else list.add(node);
   }
}

Then call this method like that:

List<File> allSdCardFiles = new ArrayList<File>();
allFiles(Environment.getExternalStorageDirectory(), allSdCardFiles);

/*now your list has all the sdcard files, use it like you want*/

Anyway, I think you understand this is a very heavy operation, so be sure you really need that (after that you also have to check the extension of your files to check if they are images, because this recursive method returns ALL sdcard files);

Upvotes: 0

BDRSuite
BDRSuite

Reputation: 1612

It seems that you are trying to make an app that shows all the images in your device and the current code shows only the mentioned directory.

use Intent to search the image type files available in your device.

Intent imageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
imageIntent.addCategory(Intent.CATEGORY_OPENABLE);
imageIntent.setType("image/*");
imageIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(imageIntent , READ_REQUEST_CODE); // set READ_REQUEST_CODE to 42 in your class

process your results in the onActivityResult() method.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {

        Uri uri = null;
        if (data != null) {
            uri = data.getData();
            Log.i(TAG, "Uri: " + uri.toString());
            showImage(uri);
        }
}

Upvotes: 2

Related Questions