我正在做一个小的库存应用程序,用户将输入一些产品的信息,然后使用相机,他将能够采取图像存储它。我正在使用SQLite存储data.My应用程序工作得很好,直到我想使用相机存储图像,然后在我的CursorAdapter我有尝试获得零数组的长度。
这是一个学习项目,我以前从来没有用过游标适配器来导入图片。我试着从SO上的帖子中找出,但失败了。
imeDelaTextView.setText(imeDela);
modeliAutomobilaTextView.setText(modeliAutomobila);
cenaTextView.setText(String.valueOf(cenaDela));
preostalaKolicinaTextView.setText(String.valueOf(preostalaKolicina));
bitmap = BitmapFactory.decodeByteArray(slikaDela, 0, slikaDela.length);//error in this line
slikaDelaImageView.setImageBitmap(bitmap); 我的光标适配器:
public class DeloviCursorAdapet extends CursorAdapter {
Bitmap bitmap;
public DeloviCursorAdapet(Context context, Cursor cursor) {
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
//LayoutInflater uzima input moj layout xml fajl odnosno, activity_main i pravi View objekat
return LayoutInflater.from(context).inflate(R.layout.list_view, viewGroup, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView imeDelaTextView = view.findViewById(R.id.ime_dela);
TextView modeliAutomobilaTextView = view.findViewById(R.id.modeli_kola);
TextView cenaTextView = view.findViewById(R.id.cena);
TextView preostalaKolicinaTextView = view.findViewById(R.id.kolicina);
ImageView slikaDelaImageView = view.findViewById(R.id.slika);
int imeDelaColumnIndex = cursor.getColumnIndex(DeoContract.DeoEntry.NAZIV_DELA);
int modeliAutomobilaColumnIndex = cursor.getColumnIndex(DeoContract.DeoEntry.MODELI_AUTOMOBILA);
int cenaColumnIndex = cursor.getColumnIndex(DeoContract.DeoEntry.CENA_DELA);
int preostalaKolicinaColumnIndex = cursor.getColumnIndex(DeoContract.DeoEntry.PREOSTALA_KOLICINA);
int slikaColumnIndex = cursor.getColumnIndex(DeoContract.DeoEntry.SLIKA_DELA);
String imeDela = cursor.getString(imeDelaColumnIndex);
String modeliAutomobila = cursor.getString(modeliAutomobilaColumnIndex);
int cenaDela = cursor.getInt(cenaColumnIndex);
int preostalaKolicina = cursor.getInt(preostalaKolicinaColumnIndex);
byte[] slikaDela = cursor.getBlob(slikaColumnIndex);
imeDelaTextView.setText(imeDela);
modeliAutomobilaTextView.setText(modeliAutomobila);
cenaTextView.setText(String.valueOf(cenaDela));
preostalaKolicinaTextView.setText(String.valueOf(preostalaKolicina));
bitmap = BitmapFactory.decodeByteArray(slikaDela, 0, slikaDela.length);//error in this line
slikaDelaImageView.setImageBitmap(bitmap);
}
}清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.korisnik.katalogdelova">
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Edit"
android:label="Izmeni deo"
android:parentActivityName=".MainActivity" />
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<!-- identifikuje ContentPtovider na uredjaju
ime klase provajder,odnsno gde se nalazi
da li je ovaj provajder deljiv sa ostalim aplikacija ovde nije i zato je false-->
<provider
android:name=".data.DeloviProvider"
android:authorities="com.example.korisnik.katalogdelova"
android:exported="false"/>
发布于 2019-06-21 04:43:49
游标为null的可能性很小,如果是这样的话,您就会在所指示的错误之前遇到错误。
不建议将图像存储在数据库中,它们基本上是臃肿的,建议的方法是将图像保存为文件,并将图像的路径存储在数据库中(或可用于从文件中检索图像的路径的一部分)。
对于Android,CursorWindow (游标中行的缓冲区)有2MB的限制。虽然大于2MB (甚至接近2MB)的图像可以保存在数据库中,但无法检索这样的图像,因为它无法放入CursorWindow中。通常,这会导致异常。
即使在1MB的情况下,您也可能一次只能检索一行。
如上所述,尝试保存图像,而不是路径,可能会导致您从一个问题到另一个问题。
This answer包括在数据库中存储高达100KB的图像的示例,但在其他情况下将图像存储为文件并将路径存储在数据库中。
This question and answer显示了一种保存大图像的方法(通过将图像分成块),但请注意,不推荐使用这种方法。
我建议使用相机拍摄的任何照片都可能太大,因此您可能希望始终将图像存储为文件,并将路径存储在数据库中。因此,第一个链接可能是最有用的。
https://stackoverflow.com/questions/56685001
复制相似问题