App crashes after selecting image in Chat (Using UiKit)

Hi

I’m using UIkit & facing this issue. I’m trying to send pictures from the gallery, app crashes after selecting an image. same thing happens in PDF & send audio files

Steps
1 Select photo & video Library option
2 Go gallery select image

Log

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://com.oneplus.filemanager/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20210516-WA0002.jpg flg=0x80000043 }} to activity {com.example.cometchattest/com.cometchat.pro.uikit.ui_components.messages.message_list.CometChatMessageListActivity}: java.lang.IllegalArgumentException: column ‘_data’ does not exist. Available columns: []
at android.app.ActivityThread.deliverResults(ActivityThread.java:5209)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5250)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2242)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:245)
at android.app.ActivityThread.main(ActivityThread.java:8004)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
Caused by: java.lang.IllegalArgumentException: column ‘_data’ does not exist. Available columns: []
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:351)
at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:91)
at com.cometchat.pro.uikit.ui_resources.utils.MediaUtils.getDataColumn(MediaUtils.java:579)
at com.cometchat.pro.uikit.ui_resources.utils.MediaUtils.getRealPathFromURI_API19(MediaUtils.java:523)
at com.cometchat.pro.uikit.ui_resources.utils.MediaUtils.getRealPath(MediaUtils.java:317)
at com.cometchat.pro.uikit.ui_components.messages.message_list.CometChatMessageList.onActivityResult(CometChatMessageList.java:1352)
at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:170)
at com.cometchat.pro.uikit.ui_components.messages.message_list.CometChatMessageListActivity.onActivityResult(CometChatMessageListActivity.java:97)
at android.app.Activity.dispatchActivityResult(Activity.java:8316)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5202)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5250)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2242)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:245)
at android.app.ActivityThread.main(ActivityThread.java:8004)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)

Hi @bharathi ,

We checked at our end on OnePlus devices and it seems to be working fine.
We think it might be device specific issue. So can you please replace below methods in MediaUtils.java file which is available in “ui_resources/utils” package and check again.

public static File getRealPath(Context context, Uri fileUri,boolean isThirdParty) {
    Log.d("", "getRealPath: " + fileUri.getPath());
    String realPath;
    if (isGoogleDrive(fileUri)) {
        return saveDriveFile(context, fileUri);
    }
    else if (isThirdParty) {
        return downloadImage(context, fileUri);
    }
    // SDK >= 11 && SDK < 19
    else if (Build.VERSION.SDK_INT < 19) {
        realPath = getRealPathFromURI_API11to18(context, fileUri);
    }
    // SDK > 19 (Android 4.4) and up
    else if (Build.VERSION.SDK_INT < 28){
        realPath = getRealPathFromURI_API19(context, fileUri);
    }
    else {
        realPath = getFilePathForN(fileUri,context);
    }

    return new File(realPath);
}

private static String getFilePathForN(Uri uri, Context context) {
        Uri returnUri = uri;
        Cursor returnCursor = context.getContentResolver().query(returnUri, null, null, null, null);
        /*
         * Get the column indexes of the data in the Cursor,
         *     * move to the first row in the Cursor, get the data,
         *     * and display it.
         * */
        int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
        returnCursor.moveToFirst();
        String name = (returnCursor.getString(nameIndex));
        String size = (Long.toString(returnCursor.getLong(sizeIndex)));
        File file = new File(context.getFilesDir(), name);
        try {
            InputStream inputStream = context.getContentResolver().openInputStream(uri);
            FileOutputStream outputStream = new FileOutputStream(file);
            int read = 0;
            int maxBufferSize = 1 * 1024 * 1024;
            int bytesAvailable = inputStream.available();

            //int bufferSize = 1024;
            int bufferSize = Math.min(bytesAvailable, maxBufferSize);

            final byte[] buffers = new byte[bufferSize];
            while ((read = inputStream.read(buffers)) != -1) {
                outputStream.write(buffers, 0, read);
            }
            Log.e("File Size", "Size " + file.length());
            inputStream.close();
            outputStream.close();
            Log.e("File Path", "Path " + file.getPath());
            Log.e("File Size", "Size " + file.length());
        } catch (Exception e) {
            Log.e("Exception", e.getMessage());
        }
        return file.getPath();
    }

Hey @darshan.bhanushali

Thanks for updating, it’s now working fine. but when I try to record an audio message not working shows (No File Found. Please)

Also, in the given code downloadImage() method is missing inside getRealPath Method

We are getting the same crashes, Let see if this works