1

对于可能发布与许多其他问题类似的问题,我感到很抱歉,但我找不到解决方案或找到与发生此错误的情况类似的情况。我整个星期都在谷歌搜索和搜索,试图找到这个问题的答案,但到目前为止还没有运气。我对android还很陌生,所以如果很明显,这可能就是原因!

这些是导致错误的步骤:

该应用程序使用相机拍照
然后将此图像存储在 SDCard 上的文件夹
中制作此文件的副本并压缩到每张图像 50-100kb(这是我要显示的内容)
然后存储加载活动(onResume 我这样做)时, sqlite 数据库中图像的初始大小和文件名
如果数据库中有任何文件名,它会获取文件名并显示图像 - 这就是错误所在.

if(c.getCount() > 0) {

   String[] cols = new String[] {DBAdapter.FILENAME,DBAdapter.DATETIME};

    int[] values = new int[] {R.id.thumbnail,R.id.datetime_stamp};

    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.image_listview, c, cols, values, 0);

    ListView listView = (ListView) findViewById(android.R.id.list);
    listView.setAdapter(cursorAdapter);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
            String row = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_ID)); // row ID from transactions
            Toast.makeText(getBaseContext(), row, Toast.LENGTH_SHORT).show();
        }
    });
}

我显然错过了一些东西,但这只是让我发疯!

如果有任何帮助,我得到的错误是:

06-13 22:17:25.269: E/AndroidRuntime(24153): FATAL EXCEPTION: main
06-13 22:17:25.269: E/AndroidRuntime(24153): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:511)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:325)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:350)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.graphics.drawable.Drawable.createFromPath(Drawable.java:831)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.ImageView.resolveUri(ImageView.java:573)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.ImageView.setImageURI(ImageView.java:350)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.support.v4.widget.SimpleCursorAdapter.setViewImage(SimpleCursorAdapter.java:197)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:143)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.AbsListView.obtainView(AbsListView.java:1451)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.ListView.makeAndAddView(ListView.java:1786)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.ListView.fillDown(ListView.java:705)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.ListView.fillGap(ListView.java:676)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:3579)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.AbsListView.onTouchEvent(AbsListView.java:2294)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.ListView.onTouchEvent(ListView.java:3493)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.View.dispatchTouchEvent(View.java:3935)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:934)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:973)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:973)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:973)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:973)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:973)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1741)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1172)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.app.Activity.dispatchTouchEvent(Activity.java:2135)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1725)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2241)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1925)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.os.Handler.dispatchMessage(Handler.java:130)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.os.Looper.loop(SourceFile:351)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.app.ActivityThread.main(ActivityThread.java:3814)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at java.lang.reflect.Method.invokeNative(Native Method)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at java.lang.reflect.Method.invoke(Method.java:538)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:659)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at dalvik.system.NativeStart.main(Native Method)

提前感谢您的帮助。

4

1 回答 1

0

首先:不要将图片保存在数据库中。将其保存到 sd 并在数据库(文件路径)中放置一个引用。数据库不是为大二进制数据而设计的(可能会大大减慢它们的速度)。

其次,我认为错误不在您提供的报价片段中。如果您从相机拍照,我假设它至少为 5MP,您必须注意不要将整张图片加载为位图,因为 5MP 图片@24bit 至少需要 114 MB 内存,但是您只有大约 24 个。您要做的是将缩小的版本加载到您的 UI 中。您首先通过以下方式获得维度:

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

然后你检查你必须缩小多少(应该是 2 的幂)并使用正确的采样选项对其进行解码

 // Calculate inSampleSize
options.inSampleSize = 8;

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);

在http://developer.android.com/training/displaying-bitmaps/load-bitmap.html阅读更多内容

于 2013-06-13T22:11:48.483 回答