0

Im trying to get images from a particular folder inside the external SD card and trying to show them inside a list when im running the app. No error provided, nothing happens. Just a blank page any suggestion.

i'm getting the list of images with the extension but how can i view the images!

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        File file = new File("/sdcard/external_sd/folder_name/");

        File imageList[] = file.listFiles();
        ArrayList<Bitmap> images = new ArrayList<Bitmap>();
        for(int i=0;i<imageList.length;i++)
        {
            Log.e("Image: "+i+": path", imageList[i].getAbsolutePath());

            Bitmap b = BitmapFactory.decodeFile(imageList[i].getAbsolutePath());

            images.add(b);

        }
         setListAdapter(new ArrayAdapter(this,
                android.R.layout.simple_list_item_1,imageList));
    }
Joe
  • 13
  • 8
  • Are you sure that the Bitmaps are decoded correctly? Consider using new File(Environment.getExternalStorageDirectory(),"folder name") as well. – Freddroid May 16 '12 at 13:19
  • setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,imageList)); I'm getting the list of the images with the extension how can i make them appear ? as an image – Joe May 16 '12 at 14:14
  • Tony the Pony here is the listView .. – Joe May 16 '12 at 14:24

1 Answers1

0

You will face with OutOfMemory exception, if you use your code to load image. Try using the method below to load image correctly. After that you can populate your ListView.

private static final int IMG_BUFFER_LEN = 16384;
// R.drawable.ic_default - default drawable resource, if we cannot load drawable from provided file
// R.dimen.list_icon_size - size in dp of ImageView inside of ListView (for example, 40dp)

private Drawable extractMediaIcon(String filePath) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    if (options.outWidth < 0 || options.outHeight < 0 || (options.outWidth * options.outHeight == 0)) {
        return context.getResources().getDrawable(R.drawable.ic_default);
    }

    final int targetHeight = dp2px((int) context.getResources().getDimension(R.dimen.list_icon_size));
    final int targetWidth = targetHeight;

    final boolean isScaleByHeight =
        Math.abs(options.outHeight - targetHeight) >= Math.abs(options.outWidth - targetWidth);

    if (options.outHeight * options.outWidth * 2 >= IMG_BUFFER_LEN) {
        // Load, scaling to smallest power of 2 that'll get it <= desired dimensions
        final double sampleSize = isScaleByHeight
                ? options.outHeight / targetHeight
                : options.outWidth / targetWidth;
        options.inSampleSize =
                (int) Math.pow(2d, Math.floor(
                        Math.log(sampleSize) / Math.log(2d)));
    }

    // Do the actual decoding
    options.inJustDecodeBounds = false;
    options.inTempStorage = new byte[IMG_BUFFER_LEN];

    Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
    if (bitmap == null) {
        return context.getResources().getDrawable(R.drawable.ic_default);
    }

    final double imageFactor = (double) bitmap.getWidth() / bitmap.getHeight();
    final double targetFactor = (double) targetWidth / targetHeight;

    bitmap = Bitmap.createScaledBitmap(
            bitmap,
            targetFactor > imageFactor ? bitmap.getWidth() * targetHeight / bitmap.getHeight() : targetWidth,
            targetFactor > imageFactor ? targetHeight : bitmap.getHeight() * targetWidth / bitmap.getWidth(),
            false);

    return new BitmapDrawable(context.getResources(), bitmap);
}
StenaviN
  • 3,687
  • 24
  • 34
  • StenaviN i tried your method didn't work for me i need to predefine the file path.. – Joe May 16 '12 at 14:14
  • Ofcourse, call `extractMediaIcon(...)` method, providing path to image file on sdcard and get back `Drawable` – StenaviN May 16 '12 at 14:31
  • StenaviN can u provide me with a setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,..)); what should be in second argument in your code! i can't find a way to show the images can you help me out – Joe May 17 '12 at 08:19
  • I don't recommend using standard adapters. I always use custom ones and perform optimization via "holder" pattern. Learn more [here](http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html) and [here](http://stackoverflow.com/q/3123193/716075). There are a lot of examples. – StenaviN May 17 '12 at 09:48
  • i managed to get all the images that are located in my gallery folder how can i filter the content in order to get only a the content of a specific folder any ideas i am using this cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, MediaStore.Images.Thumbnails._ID, // Which columns to return null, //return all rows null, MediaStore.Images.Thumbnails.IMAGE_ID); – Joe May 22 '12 at 06:37