首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在游标适配器中获取空数组的长度

尝试在游标适配器中获取空数组的长度
EN

Stack Overflow用户
提问于 2019-06-20 19:29:03
回答 1查看 117关注 0票数 0

我正在做一个小的库存应用程序,用户将输入一些产品的信息,然后使用相机,他将能够采取图像存储它。我正在使用SQLite存储data.My应用程序工作得很好,直到我想使用相机存储图像,然后在我的CursorAdapter我有尝试获得零数组的长度。

这是一个学习项目,我以前从来没有用过游标适配器来导入图片。我试着从SO上的帖子中找出,但失败了。

代码语言:javascript
复制
   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);   

我的光标适配器:

代码语言:javascript
复制
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);   

    }
}

清单

代码语言:javascript
复制
<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"/>

EN

回答 1

Stack Overflow用户

发布于 2019-06-21 04:43:49

游标为null的可能性很小,如果是这样的话,您就会在所指示的错误之前遇到错误。

  • 任何SQLiteDatabase方法(query或rawQuery)都不会返回为null的游标,如果未提取任何数据,则游标将为空,我相信在这种情况下将没有要绑定的视图。

不建议将图像存储在数据库中,它们基本上是臃肿的,建议的方法是将图像保存为文件,并将图像的路径存储在数据库中(或可用于从文件中检索图像的路径的一部分)。

对于Android,CursorWindow (游标中行的缓冲区)有2MB的限制。虽然大于2MB (甚至接近2MB)的图像可以保存在数据库中,但无法检索这样的图像,因为它无法放入CursorWindow中。通常,这会导致异常。

  • 可以容纳的大小取决于其他列,因为整行必须适合CursorWindow,因此没有确定的大小

即使在1MB的情况下,您也可能一次只能检索一行。

如上所述,尝试保存图像,而不是路径,可能会导致您从一个问题到另一个问题。

This answer包括在数据库中存储高达100KB的图像的示例,但在其他情况下将图像存储为文件并将路径存储在数据库中。

This question and answer显示了一种保存大图像的方法(通过将图像分成块),但请注意,不推荐使用这种方法。

我建议使用相机拍摄的任何照片都可能太大,因此您可能希望始终将图像存储为文件,并将路径存储在数据库中。因此,第一个链接可能是最有用的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56685001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档