简体   繁体   中英

Not Able to load images from sd card to gridview

Hi i want to load all the images that are captured from camera into a grid view. but i am getting this strange exception. if i just try to load 2 or three images all went well but when i try to load all there is the error. there are about 20 images in my sdcard .here is my code.

package com.example.imagegridview;

import java.io.File;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.widget.GridView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

    ArrayList<Bitmap> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String sdCardRootPath = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM).toString()+"/Camera";
        File rootFolder = new File(sdCardRootPath);
        File[] picFiles = rootFolder.listFiles();
        data = new ArrayList<Bitmap>();
        for (File pic:picFiles) {
            Bitmap b= BitmapFactory.decodeFile(pic.getAbsolutePath());
            data.add(b);
        }
        GridView gv = (GridView) findViewById(R.id.gridview);
        gv.setAdapter(new MAdapter(this,data));

    }

}

Adapter

package com.example.imagegridview;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class MAdapter extends BaseAdapter {

    Context c;
    ArrayList<Bitmap> b;
    public MAdapter(Context c,ArrayList<Bitmap> data){
        this.c = c;
        this.b = data;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return b.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return b.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView iv = new  ImageView(c);
        iv.setImageBitmap(b.get(arg0));
        return iv;
    }

}

layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">
<GridView
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"/>

</LinearLayout>

logCat

03-13 23:39:22.632: E/AndroidRuntime(13228): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
03-13 23:39:22.632: E/AndroidRuntime(13228):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:562)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:371)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:399)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at com.example.imagegridview.MainActivity.onCreate(MainActivity.java:32)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at android.app.ActivityThread.access$2300(ActivityThread.java:126)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at android.os.Looper.loop(Looper.java:123)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at android.app.ActivityThread.main(ActivityThread.java:4633)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at java.lang.reflect.Method.invoke(Method.java:521)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-13 23:39:22.632: E/AndroidRuntime(13228):    at dalvik.system.NativeStart.main(Native Method)

I suggest before u add images directly, u scale each image. The images from camera will actually be of a considerable size and when u try to add 20 such images, its bound to overflow on a mobile device. Therefore scale each image before u upload it and am sure it will work.

Resize bitmap before loading ie decode bitmap using below code and then that bitmap. `

     public Bitmap decodeFile(File f, int size) {

            try {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = size;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        o2.outWidth = width_tmp;
        o2.outHeight = height_tmp;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM