- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我已经创建了静态数据库并且这个数据库为图像添加了 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/
我如何知道 CursorWindow 上有多少列?为什么它有 getNumRows() 而没有 getNumColumns(),尽管有 setNumColumns()? 最佳答案 我用这种最可怕的方式
我在我的 Android 应用程序中操作 SQLite3 数据库。我刚刚从具有 20 万行和 14 列的预填充数据库中读取。条目是单词。所有列的数据类型都是文本。查询最多 11 个字母的单词(例如 A
我试图用 recyclerview 显示我的 sqlite 数据,但它给了我这个错误: 无法从 CursorWindow 读取第 0 行第 9 列。在访问游标中的数据之前,请确保游标已正确初始化。 C
我正在尝试用我的数据库 SQLite 中的数据填充一个二维数组。但是会发生以下情况: 顺便说一下,这是我第一次使用 SQLite,我试图找出是否有类似“结果集”或“数据表”的东西......我找到了所
我正在开发一个 Android 应用程序,我想在其中实现一个添加新用户的注册功能。 首先,我创建了一个名为 Registration 的 Activity ,当按下按钮时,它会调用一个添加用户的方法:
这个问题在这里已经有了答案: Could not allocate CursorWindow (3 个答案) 关闭 6 年前。 我正在开发一个 android 应用程序,因为我使用 SQLite 数
我必须执行一个查询并将结果存储在一个列表中,我使用的函数如下: List getSpoolInRecords(String label, boolean getLastInserted) {
我正在使用 AsyncStorage在 ReactNative 中在设备上存储一些数据(大尺寸>2MB),然后用下面的代码读取它 try { const value = await Asyn
我有一个应用程序,它将一些数据(9 列)存储到 SQLite 数据库中。我遵循 vogella 中的示例并用其他一些例子对其进行了调整,直到我认为应该没问题。现在,我花了两天时间努力让我的应用程序运行
我在进程之间发送/接收自定义 Query 对象。 Query 实现了 Parcelable 并包装了一个 CursorWindow 实例。首先,我通过 Messenger 将它从进程 A 发送到进程
我遇到了问题。我无法从数据库获取和设置 blob。打开我的应用程序后,我在 Logcat 中收到此错误 Caused by: java.lang.IllegalStateException: Coul
我正在尝试创建一个应用程序来从预先存在的数据库中提取 sqlite 数据,在应用程序运行之前将其复制到目录中。该数据库包含超过 2000 行和大约 20 列。 DBAdapter 类。 package
当我尝试添加到 android 上的 sqllite DB 时,我看到了上述错误。我正在使用 cloudant 库。这是堆栈跟踪: 2019-07-17 18:04:48.292 5522-5753/
我收到一些用户的以下错误。 无法从 CursorWindow 读取第 34 行第 11 列。在访问游标中的数据之前,请确保游标已正确初始化。 此问题发生在以下行中: @Override pub
我尝试使用上下文菜单从 ListView 中删除数据,但出现 java.lang.IllegalStateException: Couldn't read row 0, col 2 from Curs
我已经创建了静态数据库并且这个数据库为图像添加了 base 64 字符串这个图像是大尺寸我运行我的应用程序时数据库出错以获取字符串如何解决它。 我是 Android 编程的新手... 数据库 publ
Log Cat Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 5 from CursorWindow. Ma
我在 sqlite 数据库中插入图像时遇到困难。我的代码中没有语法错误。我运行它后,应用程序自动强制停止。 logcat 显示: 由以下原因引起:java.lang.IllegalStateExcep
我以前使用过 SQLDB,但只保存了 3 项。现在我想添加更多内容,但出现此错误,但无法解决。我不明白我的光标在哪里变坏并且不再工作了。谁能解释一下为什么我现在会收到此错误?非常感谢。 缺席.java
我在执行我的代码时遇到此错误。 Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized c
我是一名优秀的程序员,十分优秀!