java.lang.IllegalArgumentException: the bind value at index 1 is null

Keywords: Java Database Android SQLite

java.lang.IllegalArgumentException: the bind value at index 1 is null
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)

Custom cameras take many pictures, save them locally and add them to the system album, then browse the captured pictures in the customized album and delete the first one. Delete the second one and report the abnormality above.

In addition to deleting the local image file, the method of deleting the image also deletes the data in the database. See if the information is null. View logs:

E/PhotoAlbumDetailActvty2: deleteImage,imgRealPath:/storage/emulated/0/faceallRecog/IMG_20171204_214942.jpg,imgPathcontent://media/external/images/media/27706
E/PhotoAlbumDetailActvty2: deleteImage,imgRealPath:null,imgPathcontent://media/external/images/media/27706
The first line is the first picture deleted, and the second line is the second picture deleted. You can see that imgRealPath is null, and imgPath is the first picture.

Look at the deletion method:

private void deleteImage(String imgPath) {
        String imgRealPath = getRealPathFromUri(activity, Uri.parse(imgPath));
        Log.e(TAG, "deleteImage,imgRealPath:" + imgRealPath + ",imgPath" + imgPath);
        ContentResolver resolver = activity.getContentResolver();
        Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                new String[]{imgRealPath}, null);
It is known that the parameter imgPath passed in when deleting the second picture is the path of the first picture, and the first picture has been deleted, resulting in the imgRealPath empty.
I use the picture shown by viewPager. After deleting the picture, viewPager automatically displays the next picture.

In the above description, the imgPath in the deleteImage(String imgPath) after deletion is still the deleted image path, which will report the above exception when deletion continues.

It is now amended to read:

private int selectIndex;//Subscripts for selected pictures

private void getImgUriStr(final int index) { //This method is called in adapter, gets index data from the adapter click, and passes it to selectIndex.
        selectIndex=index;
        Log.e("selectIndex",selectIndex+"");

 //Delete pictures
    private void deleteImage() {
        //Solve the problem of crash when a custom camera takes more than one picture and looks at the picture and deletes more than one picture. Deleting pictures will change the total number of pictures, and pass in selectIndex to get the latest picture uri.
        selectImgUri = currentLabelList.get(selectIndex).getOriginalUri();
        imgId = currentLabelList.get(selectIndex).getMediaID();
        String imgRealPath = getRealPathFromUri(activity, Uri.parse(selectImgUri));
        Log.e(TAG, "deleteImage,imgRealPath:" + imgRealPath + ",selectImgUri:"+selectImgUri);
        ContentResolver resolver = activity.getContentResolver();
        Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                new String[]{imgRealPath}, null);
        boolean result = false;
        if (cursor!=null&&cursor.moveToFirst()) {
            long id = cursor.getLong(0);
            Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            Uri uri = ContentUris.withAppendedId(contentUri, id);
            int count = activity.getContentResolver().delete(uri, null, null);
            activity.getContentResolver().notifyChange(uri, null);
            result = count == 1;
        } else {
            File file = new File(imgRealPath);
            result = file.delete();
        }
        //If the image to be deleted is a collection image, it should be deleted from the collection table.
        if (favFlag) {
            helper.deleteFavouritesImg(imgId);
        }

        if (result) {
            //Delete and refresh the image from the list
            removeFromList(currentLabelList, imgId);
            //This method cannot delete elements in the list
//            currentLabelList.remove(imgPath);
            myAdapter.notifyDataSetChanged();
            viewPagerAdapter.notifyDataSetChanged();
            showToast(activity, "Successful deletion");
        }
        //Resolve bug s that do not change the total number of top pictures after deleting pictures.
        tvCountView.setText((selectIndex + 1) + "/" + currentLabelList.size());
    }
Get the latest image data, then delete it, and solve the problem above.


Posted by hesketh on Sun, 10 Feb 2019 18:18:17 -0800