Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的应用程序在显示数据库中存储的某些图像时崩溃?_Java_Android - Fatal编程技术网

Java 我的应用程序在显示数据库中存储的某些图像时崩溃?

Java 我的应用程序在显示数据库中存储的某些图像时崩溃?,java,android,Java,Android,因此,我的应用程序通过SQLite使用blob保存图像。我的应用程序按预期运行,并通过cursorAdaptor和bindView方法将数据显示到listView 但是,如果我在设备中保存了一些图像,可能是相机拍摄的照片和本地保存的照片,当我的应用程序试图在列表视图中显示该列表项时就会崩溃。如果该行在屏幕上不可见,它将显示所有其他列表项,并在向下滚动列表时崩溃 可能原因:图像文件大小大于400kb FATAL EXCEPTION: main

因此,我的应用程序通过SQLite使用blob保存图像。我的应用程序按预期运行,并通过
cursorAdaptor
bindView
方法将数据显示到
listView

但是,如果我在设备中保存了一些图像,可能是相机拍摄的照片和本地保存的照片,当我的应用程序试图在
列表视图中显示该列表项时就会崩溃。如果该行在屏幕上不可见,它将显示所有其他列表项,并在向下滚动列表时崩溃

可能原因:图像文件大小大于400kb

FATAL EXCEPTION: main
                                                                               Process: com.agoogler.rafi.inventory10, PID: 29622
                                                                               java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                                                   at android.database.CursorWindow.nativeGetLong(Native Method)
                                                                                   at android.database.CursorWindow.getLong(CursorWindow.java:524)
                                                                                   at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
                                                                                   at android.database.CursorWrapper.getLong(CursorWrapper.java:127)
                                                                                   at android.widget.CursorAdapter.getItemId(CursorAdapter.java:259)
                                                                                   at android.widget.AbsListView$RecycleBin.retrieveFromScrap(AbsListView.java:10185)
                                                                                   at android.widget.AbsListView$RecycleBin.getScrapView(AbsListView.java:9894)
                                                                                   at android.widget.AbsListView.obtainView(AbsListView.java:3234)
                                                                                   at android.widget.ListView.makeAndAddView(ListView.java:2147)
                                                                                   at android.widget.ListView.fillDown(ListView.java:767)
                                                                                   at android.widget.ListView.fillGap(ListView.java:731)
                                                                                   at android.widget.AbsListView.trackMotionScroll(AbsListView.java:8293)
                                                                                   at android.widget.ListView.trackMotionScroll(ListView.java:2065)
                                                                                   at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:4905)
                                                                                   at android.widget.AbsListView.startScrollIfNeeded(AbsListView.java:4833)
                                                                                   at android.widget.AbsListView.onTouchMove(AbsListView.java:6648)
                                                                                   at android.widget.AbsListView.onTouchEvent(AbsListView.java:6492)
                                                                                   at android.widget.ListView.onTouchEvent(ListView.java:1648)
                                                                                   at android.view.View.dispatchTouchEvent(View.java:10723)
                                                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2859)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2535)
                                                                                   at android.widget.AbsListView.dispatchTouchEvent(AbsListView.java:6401)
                                                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
                                                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
                                                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
                                                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
                                                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
                                                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2865)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2550)
                                                                                   at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:559)
                                                                                   at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1870)
                                                                                   at android.app.Activity.dispatchTouchEvent(Activity.java:3236)
                                                                                   at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
                                                                                   at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
                                                                                   at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:521)
                                                                                   at android.view.View.dispatchPointerEvent(View.java:10952)
                                                                                   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5117)
                                                                                   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4969)
                                                                                   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4500)
                                                                                   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4553)
                                                                                   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4519)
                                                                                   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4652)
                                                                                   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4527)
                                                                                   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4709)
                                                                                   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4500)
                                                                                   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4553)
06-30 18:22:34.950 29622-29622/com.agoogler.rafi.inventory10 E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4519)
                                                                                   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4527)
                                                                                   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4500)
                                                                                   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7007)
                                                                                   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6936)
                                                                                   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6897)
                                                                                   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7117)
                                                                                   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                                                                                   at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
                                                                                   at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
                                                                                   at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7081)
                                                                                   at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:7144)
                                                                                   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
                                                                                   at android.view.Choreographer.doCallbacks(Choreographer.java:702)
                                                                                   at android.view.Choreographer.doFrame(Choreographer.java:632)
                                                                                   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
                                                                                   at android.os.Handler.handleCallback(Handler.java:751)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6682)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
游标适配器类

public class InventoryCursorAdapter extends CursorAdapter {

    Context mcontext;

    /**
     * Constructs a new {@link InventoryCursorAdapter}.
     *
     * @param context The context
     * @param c       The cursor from which to get the data.
     */
    public InventoryCursorAdapter(Context context, Cursor c) {

        super(context, c, 0 /* flags */);
        mcontext = context;
    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // Inflate a list item view using the layout specified in list_item.xml
        return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
    }


    @Override
    public void bindView(View view, Context context, final Cursor cursor) {
        // Find individual views that we want to modify in the list item layout
        TextView nameTextView = (TextView) view.findViewById(R.id.name);
        TextView quanTextView = (TextView) view.findViewById(quantity);
        TextView quanText = (TextView) view.findViewById(quantitytext);
        ImageView imageView = (ImageView) view.findViewById(R.id.photo_image_view);

        Button button = (Button) view.findViewById(R.id.button);
        int idColumnIndex = cursor.getColumnIndex(_ID);
        Long id = cursor.getLong(idColumnIndex);
        button.setTag(id);   // tagging the button of the list item with the row id of the data item
        Log.d("test id of the row", String.valueOf(id));


        // Find the columns of pet attributes that we're interested in
        int nameColumnIndex = cursor.getColumnIndex(COLUMN_PRO_NAME);
        int priceColumnIndex = cursor.getColumnIndex(COLUMN_PRO_PRICE);
         int quanColumnIndex = cursor.getColumnIndex(COLUMN_PRO_QUAN);
        int supColumnIndex = cursor.getColumnIndex(COLUMN_PRO_SUP);
        int imageColumnIndex = cursor.getColumnIndex(COLUMN_IMAGE);


        // Read the pet attributes from the Cursor for the current inventory
        String invName = cursor.getString(nameColumnIndex);
        String price = cursor.getString(priceColumnIndex);
        String quantity = cursor.getString(quanColumnIndex);
        String supplier = cursor.getString(supColumnIndex);
        Bitmap image = Utility.getImage(cursor.getBlob(imageColumnIndex));


        if (TextUtils.isEmpty(invName)) {
            invName = context.getString(R.string.unknown_name);
        }

        // Update the TextViews with the attributes for the current pet
        nameTextView.setText(invName);
        quanTextView.setText(quantity);
        if (Integer.parseInt(quantity) <= 3) {
            quanTextView.setTextColor(ContextCompat.getColor(mcontext, R.color.red));
            quanText.setTextColor(ContextCompat.getColor(mcontext, R.color.red));

        }
        if (image != null)
            imageView.setImageBitmap(image);


        Button clickButton = (Button) view.findViewById(R.id.button);
        clickButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.i("test", "Button onclick executing");
                long id = (Long) v.getTag();  // getting the tag of the button
                Log.d("test id of button tag", String.valueOf(id));
                Uri currentInvUri = ContentUris.withAppendedId(CONTENT_URI, id);

                String[] projection = {
                        _ID,
                        COLUMN_PRO_NAME, COLUMN_PRO_SUP, COLUMN_PRO_QUAN,
                        COLUMN_PRO_PRICE,COLUMN_IMAGE};

                Cursor cursor1 = mcontext.getContentResolver().query(currentInvUri, projection, null, null, null);

                int quanColumnIndex = cursor1.getColumnIndex(COLUMN_PRO_QUAN);
                cursor1.moveToFirst();

                int quantity = cursor1.getInt(quanColumnIndex);


                ContentValues values = new ContentValues();
                if (quantity == 0) {
                    values.put(COLUMN_PRO_QUAN, quantity);
                    String textToShow = "Cannot complete sale";
                    Toast.makeText(mcontext, textToShow, Toast.LENGTH_SHORT).show();
                } else {
                    values.put(COLUMN_PRO_QUAN, --quantity);
                }


                int rowsAffected = mcontext.getContentResolver().update(currentInvUri, values, null, null);


                if (rowsAffected == 0) {
                    Log.i("test", "0 rows updated");

                } else {

                    Log.i("test", " rows updated");

                }


            }
        });

    }
}
公共类InventoryCursorAdapter扩展了CursorAdapter{
语境;
/**
*构造一个新的{@link InventoryCursorAdapter}。
*
*@param context上下文
*@param c从中获取数据的光标。
*/
公共资源清册游标适配器(上下文,游标c){
超级(上下文,c,0/*标志*/);
mcontext=上下文;
}
@凌驾
公共视图newView(上下文上下文、光标、视图组父对象){
//使用list_item.xml中指定的布局展开列表项视图
返回LayoutInflater.from(上下文)。充气(R.layout.list\u项,父项,false);
}
@凌驾
公共void bindView(视图视图、上下文上下文、最终光标){
//在列表项布局中查找要修改的各个视图
TextView name TextView=(TextView)view.findViewById(R.id.name);
TextView quanTextView=(TextView)view.findViewById(数量);
TextView quanText=(TextView)view.findViewById(quantitytext);
ImageView ImageView=(ImageView)view.findViewById(R.id.photo\u image\u view);
Button Button=(Button)view.findViewById(R.id.Button);
int-idColumnIndex=cursor.getColumnIndex(_-ID);
Long id=cursor.getLong(idColumnIndex);
setTag(id);//用数据项的行id标记列表项的按钮
Log.d(“行的测试id”,String.valueOf(id));
//查找我们感兴趣的pet属性列
int nameColumnIndex=cursor.getColumnIndex(COLUMN\u PRO\u NAME);
int priceColumnIndex=cursor.getColumnIndex(COLUMN\u PRO\u PRICE);
int quanColumnIndex=cursor.getColumnIndex(COLUMN\u PRO\u QUAN);
int supColumnIndex=cursor.getColumnIndex(COLUMN\u PRO\u SUP);
int-imageColumnIndex=cursor.getColumnIndex(COLUMN\u IMAGE);
//从光标读取当前库存的pet属性
String invName=cursor.getString(nameColumnIndex);
String price=cursor.getString(priceColumnIndex);
字符串数量=cursor.getString(quanColumnIndex);
字符串供应商=cursor.getString(supColumnIndex);
位图image=Utility.getImage(cursor.getBlob(imageColumnIndex));
if(TextUtils.isEmpty(invName)){
invName=context.getString(R.string.unknown_name);
}
//使用当前宠物的属性更新TextView
nameTextView.setText(invName);
quanTextView.setText(数量);
if(整数.parseInt(数量)