我已经创建了静态数据库,这个数据库为图像添加了基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保存在数据库中,这样做更快、更易于管理。
如果你真的需要用你的方式去做,不管是什么原因…尝试在android:largeHeap="true"
中设置AndroidManifest.xml
。
更新:
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
关于java - android中的android.database.CursorWindow.getString处的java.lang.OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40648190/