Xaba Aslam
Xaba Aslam

Reputation: 1

To load images from Folder placed in Assets Folder using androidStudio

Using Android Studio. I created Asset folder in app/main/src . I paste the images in assets folder they are easily accessible. But when I created an other folder in assets folder named "Files" and try to read that images. They are not shown . Please help me to read that images that are placed in folder named "files" in assets folder.

  ImageView img = (ImageView) findViewById(R.id.img);
  AssetManager manager = getAssets();
  try {
        String[] file = manager.list("Files");
        for (int i = 0; i < file.length; i++) {
                  try {
        InputStream imgStream ;
        imgStream = manager.open(file[i]);
        Bitmap bitmap = BitmapFactory.decodeStream(imgStream);
        img.setImageBitmap(bitmap);

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

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

Upvotes: 0

Views: 1957

Answers (2)

Nash Makineta
Nash Makineta

Reputation: 79

I know this is a late reply, but I was trying the same as you... The only little bit missing was the file route in the function open. Also it didn't work for me with a Bitmap, but with a Drawable. Try this below, it should work. :)

ImageView img = (ImageView) findViewById(R.id.img);
  AssetManager manager = getAssets();
  try {
        String[] file = manager.list("Files");
        for (int i = 0; i < file.length; i++) {
            try {
                InputStream imgStream ;
                imgStream = manager.open("Files/" + file[i]);
                Drawable d = Drawable.createFromStream(imgStream, null);
                im.setImageDrawable(d);

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

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

Upvotes: 0

Kostas Drak
Kostas Drak

Reputation: 3260

try to do this. Load this method anywhere you want to use it

public void loadDataFromAsset() {

        // load image
        try {
            // get input stream
            InputStream ims = getAssets().open("your_image.extension");
            // load image as Drawable
            Drawable d = Drawable.createFromStream(ims, null);
            // set image to ImageView
            yourImageView.setImageDrawable(d);
        }
        catch(IOException ex) {
            return;
        }

    }

I hope it helps!!!

Upvotes: 3

Related Questions