0

How I can get the image from the Assets folder and not from android.os.Environment.getExternalStorageDirectory()? I need to replace this code as per my requirement:

File directory = new File(
            android.os.Environment.getExternalStorageDirectory()
                    + File.separator + AppConstant.PHOTO_ALBUM);

Thanks

EDIT:

ArrayList filePaths = new ArrayList();

if (directory.isDirectory()) {

        File[] listFiles = directory.listFiles();

        if (listFiles.length > 0) {

            for (int i = 0; i < listFiles.length; i++) {

                String filePath = listFiles[i].getAbsolutePath();

                if (IsSupportedFile(filePath)) {

                    filePaths.add(filePath);
                }
            }
        } else {
            Toast.makeText(
                    _context,
                    AppConstant.PHOTO_ALBUM
                            + " is empty. Please load some images in it !",
                    Toast.LENGTH_LONG).show();
        }

    } else {
        AlertDialog.Builder alert = new AlertDialog.Builder(_context);
        alert.setTitle("Error!");
        alert.setMessage(AppConstant.PHOTO_ALBUM
                + " directory path is not valid! Please set the image directory name AppConstant.java class");
        alert.setPositiveButton("OK", null);
        alert.show();
    }
user3231711
  • 89
  • 3
  • 9

1 Answers1

0

here is the way, how to get all images in a known asset subfolder. Only thing you need to know is the name of the subfolder with images in asset. Hope it will help.

private List<Bitmap> imageListFromAsset(String dirFrom) {
    //list of images in assets
    List<Bitmap> imageList = new ArrayList<Bitmap>();

    Resources res = getResources();
    AssetManager am = res.getAssets();
    String fileList[];
    try {
        fileList = am.list(dirFrom);

        for (String fileName : fileList) {
            imageList.add(getImageFromAsset(dirFrom+ File.separator +fileName, am));
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    return imageList;

}

private Bitmap getImageFromAsset(String path, AssetManager assetManager) {
    InputStream istr = null;
    try {
        istr = assetManager.open(path);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(istr);
    return bitmap;
}

If you need only paths to these images just stop atfileList = am.list(dirFrom);. Don't forget to add dirName before fileName.

EDIT as per request in comment:

Resources res = getResources();
AssetManager am = res.getAssets();
String fileList[];
try {
    fileList = am.list(dirFrom);

    for (String fileName : fileList) {
        fileName = dirFrom + File.separator + fileName;
    }

} catch (IOException e) {
    e.printStackTrace();
}

Now you have all filePaths in assets/dirName. Now conversion to ArrayList;

List<String> imagePaths = new ArrayList<String>(Arrays.asList(fileList));

And now the image paths are in the list.

Alex
  • 688
  • 6
  • 8
  • I need a ArrayList of (path + nameImage) from Assets – user3231711 Jan 29 '14 at 12:48
  • Edited original answer. – Alex Jan 29 '14 at 13:14
  • yes, i only need the paths of all images of Assets/myFolder. i need a ArrayList with the paths. help me please – user3231711 Jan 29 '14 at 13:19
  • String fileList[] = am.list(dirFrom); - this will give you fileNames of all images in subfolder dirName in assets. So you have to move all your images to assets/[dirName]/ Then you can get those files using assetmanager as described in answer. Is that clear? To convert String[] to List: http://stackoverflow.com/a/10530379/1876084 – Alex Jan 29 '14 at 13:25
  • No. i have edited my code. I need replace the new code (external storage) for a ArrayList from Assets/myFolder. – user3231711 Jan 29 '14 at 13:29
  • I added the edit part. You can add Toast with messages if dir is empty or so. – Alex Jan 29 '14 at 14:27
  • I have used your code and it not works but i dont know the problem. i´m trying to use this code: http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/ Do you can download this code and you try the code works with images from assets? – user3231711 Jan 29 '14 at 15:02