从数据库显示一些存储的图像时我的应用程序崩溃了?

时间:2017-06-30 13:10:00

标签: java android

所以,我的应用程序通过SQLite使用blob保存图像。我的应用按预期运行,并将数据显示为listViewcursorAdaptorbindView方法。

但是,如果我在我的设备中保存了一些图像,可能是我的相机拍摄并保存在本地,我的应用程序在尝试在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)

CursorAdapter类

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");

                }


            }
        });

    }
}

0 个答案:

没有答案