从图库以编程方式挑选图像

时间:2014-02-26 13:28:52

标签: android bitmap cursor

我尝试使用以下代码以编程方式从sdcard获取图像。但我得到了“outofmemoryerror”。请帮帮我。

public class MainActivity extends Activity {
    private ImageView image;
    public static final String uri = "content://media/external/images/media";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.imageView1);
        Uri path = Uri.parse(uri);
        String[] filepath = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(path, filepath, null, null,
                null);
        cursor.moveToFirst();
        String imagepath = cursor.getString(cursor.getColumnIndex(filepath[0]));
        Log.i("siva", "siva" + imagepath);
        image.setImageBitmap(BitmapFactory.decodeFile(imagepath));

    }

}

logcat:

02-26 18:35:18.069: D/dalvikvm(25010): GC_FOR_ALLOC freed 52K, 9% free 9508K/10371K, paused 16ms, total 16ms
02-26 18:35:18.159: I/dalvikvm-heap(25010): Grow heap (frag case) to 29.244MB for 20155408-byte allocation
02-26 18:35:18.629: D/dalvikvm(25010): GC_CONCURRENT freed 1K, 3% free 29190K/30087K, paused 3ms+37ms, total 468ms
02-26 18:35:18.629: D/dalvikvm(25010): WAIT_FOR_CONCURRENT_GC blocked 1ms
02-26 18:35:19.060: D/libEGL(25010): loaded /system/lib/egl/libGLES_hawaii.so
02-26 18:35:19.070: D/(25010): mem_init ++
02-26 18:35:19.070: D/(25010): gHwMemAllocator client 3
02-26 18:35:19.070: D/(25010): **** Using ION allocator ****
02-26 18:35:19.070: D/(25010): registered SIGUSR1[10] for pid[25010]
02-26 18:35:19.070: D/(25010): HwMemAllocatorImpl Static Counters 0 0
02-26 18:35:19.070: D/(25010): HwMemAllocatorImpl[4adcad5c] totalDeviceAllocSize[0] totalFree[0] maxFree[0] in numSlabs[0]
02-26 18:35:19.080: D/(25010): mem_init 4adcad5c--
02-26 18:35:19.080: D/ION(25010): config: version(0x10000) secure(0xf000) 256M(0x22d) fast(0x608) hwwr(0x608)
02-26 18:35:19.080: D/(25010): Waiting for mm thread to come up
02-26 18:35:19.080: D/(25010): Waiting for mm thread to come up
02-26 18:35:19.080: D/(25010): mm_device_thread starting
02-26 18:35:19.080: D/HAWAII_EGL(25010): eglCreateContext() config: 35 context: 0x4c92a450, VC context 1, Thread 25010
02-26 18:35:19.090: D/HAWAII_EGL(25010): Set SWAP INTERVAL 0
02-26 18:35:19.090: D/HAWAII_EGL(25010): eglCreateWindowSurface() surface: 0x4d0618a0, VC surface: 1, Thread: 25010
02-26 18:35:19.090: D/HAWAII_EGL(25010): eglMakeCurrent(0x4c92a450, 0x4d0618a0, 0x4d0618a0) Thread: 25010
02-26 18:35:19.100: D/OpenGLRenderer(25010): Enabling debug mode 0
02-26 18:35:19.720: W/OpenGLRenderer(25010): Bitmap too large to be uploaded into a texture (2592x1944, max=2048x2048)
02-26 18:35:19.760: D/HAWAII_EGL(25010): eglMakeCurrent(0x4c92a450, 0x4d286310, 0x4d286310) Thread: 25010
02-26 18:35:19.760: D/HAWAII_EGL(25010): eglMakeCurrent(NULL) Thread: 25010
02-26 18:35:22.613: D/HAWAII_EGL(25010): eglMakeCurrent(0x4c92a450, 0x4d286310, 0x4d286310) Thread: 25010
02-26 18:35:22.613: D/HAWAII_EGL(25010): eglMakeCurrent(NULL) Thread: 25010
02-26 18:35:23.214: D/HAWAII_EGL(25010): eglMakeCurrent(0x4c92a450, 0x4d0618a0, 0x4d0618a0) Thread: 25010
02-26 18:35:23.704: D/HAWAII_EGL(25010): eglMakeCurrent(NULL) Thread: 25010
02-26 18:35:23.704: D/HAWAII_EGL(25010): eglDestroySurface() surface: 0x4d0618a0, android window 0x4c925438, Thread: 25010
02-26 18:35:24.104: D/dalvikvm(25010): GC_FOR_ALLOC freed 48K, 3% free 29259K/30087K, paused 19ms, total 22ms
02-26 18:35:24.104: I/dalvikvm-heap(25010): Forcing collection of SoftReferences for 20155408-byte allocation
02-26 18:35:24.125: D/dalvikvm(25010): GC_BEFORE_OOM freed 11K, 3% free 29247K/30087K, paused 22ms, total 23ms
02-26 18:35:24.125: E/dalvikvm-heap(25010): Out of memory on a 20155408-byte allocation.
-2602 18:35:24.135: I/dalvikvm(25010): "main" prio=5 tid=1 RUNNABLE
02-26 18:35:24.135: I/dalvikvm(25010):   | group="main" sCount=0 dsCount=0 obj=0x40dbe508 self=0x40dae228
02-26 18:35:24.135: I/dalvikvm(25010):   | sysTid=25010 nice=0 sched=0/0 cgrp=default handle=1074786096
02-26 18:35:24.135: I/dalvikvm(25010):   | schedstat=( 0 0 0 ) utm=56 stm=18 core=0
02-26 18:35:24.135: I/dalvikvm(25010):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
02-26 18:35:24.165: I/dalvikvm(25010):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:663)
02-26 18:35:24.165: I/dalvikvm(25010):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:392)
02-26 18:35:24.165: I/dalvikvm(25010):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:457)
02-26 18:35:24.165: I/dalvikvm(25010):   at com.example.sivareddy1.MainActivity.onCreate(MainActivity.java:29)
02-26 18:35:24.165: I/dalvikvm(25010):   at android.app.Activity.performCreate(Activity.java:5207)
02-26 18:35:24.185: I/dalvikvm(25010):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
02-26 18:35:24.195: I/dalvikvm(25010):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
02-26 18:35:24.195: I/dalvikvm(25010):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
02-26 18:35:24.195: I/dalvikvm(25010):   at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3590)
02-26 18:35:24.195: I/dalvikvm(25010):   at android.app.ActivityThread.access$800(ActivityThread.java:140)
02-26 18:35:24.195: I/dalvikvm(25010):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
02-26 18:35:24.195: I/dalvikvm(25010):   at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 18:35:24.205: I/dalvikvm(25010):   at android.os.Looper.loop(Looper.java:177)
02-26 18:35:24.205: I/dalvikvm(25010):   at android.app.ActivityThread.main(ActivityThread.java:4947)
02-26 18:35:24.205: I/dalvikvm(25010):   at java.lang.reflect.Method.invokeNative(Native Method)
02-26 18:35:24.215: I/dalvikvm(25010):   at java.lang.reflect.Method.invoke(Method.java:511)
02-26 18:35:24.225: I/dalvikvm(25010):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-26 18:35:24.225: I/dalvikvm(25010):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-26 18:35:24.225: I/dalvikvm(25010):   at dalvik.system.NativeStart.main(Native Method)
02-26 18:35:24.225: W/CursorWrapperInner(25010): Cursor finalized without prior close()
02-26 18:35:24.225: D/skia(25010): --- decoder->decode returned false
02-26 18:35:24.265: D/AndroidRuntime(25010): Shutting down VM
02-26 18:35:24.265: W/dalvikvm(25010): threadid=1: thread exiting with uncaught exception (group=0x40dbd2a0)
02-26 18:35:24.275: E/AndroidRuntime(25010): FATAL EXCEPTION: main
02-26 18:35:24.275: E/AndroidRuntime(25010): java.lang.OutOfMemoryError
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:663)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:392)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:457)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at com.example.sivareddy1.MainActivity.onCreate(MainActivity.java:29)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.app.Activity.performCreate(Activity.java:5207)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3590)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.app.ActivityThread.access$800(ActivityThread.java:140)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.os.Looper.loop(Looper.java:177)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at android.app.ActivityThread.main(ActivityThread.java:4947)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at java.lang.reflect.Method.invokeNative(Native Method)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at java.lang.reflect.Method.invoke(Method.java:511)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-26 18:35:24.275: E/AndroidRuntime(25010):    at dalvik.system.NativeStart.main(Native Method)
02-26 18:35:24.385: W/CursorWrapperInner(25010): Binder has been finalized, ise=java.lang.IllegalStateException: Binder has been finalized!

1 个答案:

答案 0 :(得分:0)

在Android上,在内存中加载位图是非常敏感的。你必须关心图片大小。内存分配不取决于实际文件权重,而是取决于图片本身的大小。

这是一个很好的讲座,你应该考虑阅读Android,图片加载和内存效率:

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html