Pregunta

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();
    }
¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top