android.database.CursorWindow.getString 中的 java.lang.OutOfMemoryError

标签 java android database bitmap base64

我已经创建了静态数据库并且这个数据库为图像添加了 base 64 字符串这个图像是大尺寸我运行我的应用程序时数据库出错以获取字符串如何解决它。
我是 Android 编程的新手...

数据库

public List<People> getAllPeople() {

    List<People> peoples = new ArrayList<>();

    try {
        SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
        Cursor cursor = db.rawQuery("select * from " + TABLE_PEOPLE, null);
        while (cursor.moveToNext()) {
            if (cursor != null) {
                String peopleImage = cursor.getString(cursor.getColumnIndex(PEOPLE_IMAGE));\\ This line getting error
                String categoryId = cursor.getString(cursor.getColumnIndex(CATEGORY_ID));
                String peopleName = cursor.getString(cursor.getColumnIndex(PEOPLE_NAME));
                String peopleId = cursor.getString(cursor.getColumnIndex(ID));
                int status = cursor.getInt(cursor.getColumnIndex(STATUS));
                String month = cursor.getString(cursor.getColumnIndex(PEOPLE_MONTH));
                String date = cursor.getString(cursor.getColumnIndex(PEOPLE_DATE));
                String year = cursor.getString(cursor.getColumnIndex(PEOPLE_YEAR));
                String peopleDetail = cursor.getString(cursor.getColumnIndex(PEOPLE_DETAIL));


                People people = new People();
                people.setId(peopleId);
                people.setPeopleName(peopleName);
                people.setPeopleImage(peopleImage);
                people.setStatus(status);
                people.setMonth(month);
                people.setDate(date);
                people.setYear(year);
                people.setPeopleDetail(peopleDetail);
                people.setCategoryId(categoryId);

                peoples.add(people);
            }
        }
    } catch (Exception e) {
        Log.d("DB", e.getMessage());
    }
    return peoples;
}

适配器

byte[] decodedString = Base64.decode(people.getPeopleImage(), Base64.DEFAULT);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length, options);
    options.inSampleSize = calculateInSampleSize(options, 100, 100);
    options.inJustDecodeBounds = false;
    Bitmap bmp1 = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length, options);
    if (bmp1 != null) {
        holder.ivPeopleImage.setImageBitmap(bmp1);
    }

最佳答案

就个人而言,我认为将图像存储在数据库中不是一个好主意。将它们保存在 Base64 中会使您的解决方案更慢,每次操作它们时都需要进行编码和解码。我的建议是将图像文件保存在存储介质上并将 URI 保存在数据库中,这样速度更快,更易于管理。

如果您出于某种原因确实需要按照自己的方式进行...尝试在 AndroidManifest.xml 中设置 android:largeHeap="true"

更新:

  private static final String FOLDER="/MyApp/";  
  private static String saveBitmapToSd(Bitmap bmp, String fileName){
        String pngName=Environment.getExternalStorageDirectory().toString()+FOLDER+fileName+".png";
        FileOutputStream fos= null;
        boolean success=false;
        try {
            fos = new FileOutputStream(new File(pngName));
            bmp.compress(Bitmap.CompressFormat.PNG,100,fos);
            success=true;
        } catch (FileNotFoundException e) {
            Log.e(TAG, "saveBitmapToSd: ", e);
        }
        finally {
            if(fos!=null)
                try {
                    fos.close();
                } catch (IOException e) {
                    Log.e(TAG, "saveBitmapToSd: ",e );
                }
        }
        if(success)
            return pngName;
        else
            return null;
    }

这是将位图保存到sd卡并返回路径字符串的方法。 我刚刚写了一个非常简单的演示应用程序,你可以在这里查看。 Demo App

关于android.database.CursorWindow.getString 中的 java.lang.OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40648190/

相关文章:

java - 如何在代码中表达这个特殊的数组断言?

java - 在 php 中使用 js api 使用 Evercookie 创建 cookie 时出现问题

Android:如何获取已安装 Activity 的列表,就像它们出现在启动器中一样,没有重复

带有自动滚动指示器的 Android View 寻呼机

phpMyAdmin: "URI too large"

sql-server - 在 SQL Server 数据库中自动添加对象及其关系

java - 使用 doclava 将 Logo 添加到 javadoc 的左上角

c# - 从 C# 启动 Java 平台

java - 仅显示 MPAndroidChart 中的部分值

php - 如何连接到 XAMPP 服务器?我收到错误消息 Fatal error : Array callback has to contain indices 0 and 1