Question Detail

ERROR FOUND null: open failed: ENOENT

1 years ago Views 334 Visit Post Reply


Thread Reply

Hemant Haritash

- 1 years ago

private String getRealPathFromUri(Uri uri) {
    String[] projection = {MediaStore.Images.Media.DATA};
    CursorLoader loader = new CursorLoader(getContext(), uri, projection, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int Column_idx = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String result = cursor.getString(Column_idx);
    cursor.close();
    return result;
}

public String getRealPathFromURI(Uri uri) {
    String path = "";
    if (getActivity().getContentResolver() != null) {
        Cursor cursor = getContext().getContentResolver().query(uri, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            path = cursor.getString(idx);
            cursor.close();
        }
    }
    return path;
}

Hemant Haritash

- 1 years ago

if (Objects.requireNonNull(data).getClipData() != null) {
    int count = Objects.requireNonNull(data.getClipData()).getItemCount();
    isLogPrintValue("ImagesSize", "Count: " + count);
    int currentItem = 0;
    while (currentItem < count) {
        Uri imageUri = data.getClipData().getItemAt(currentItem).getUri();
        currentItem = currentItem + 1;
        try {
            String filePath = getRealPathFromURI(mActivity, imageUri);
            selectedimagePath = filePath;
            isLogPrintValue("UriSelected", "" + filePath);

            if (imageUri != null) {
                findViewById(R.id.attach_file_name).setVisibility(View.VISIBLE);
                fileNameTxtView.setText(imageUri.getPath());
            }
            String ext = filePath.substring(filePath.lastIndexOf(".") + 1);
            if (!ext.equals("webp") && filePath.lastIndexOf(".") != -1) {
                isLogPrintValue("FinalUriSelected", "" + filePath);
                //  cropImageMethod(filePath, mActivity);
            } else {
                Toast.makeText(mActivity, mActivity.getString(R.string.str_InvalidImageType), Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            isLogPrintValue("TAG", "Fileselecterror" + e);
            e.printStackTrace();
        }
    }
} else 
    if (data.getData() != null) {
    final Uri uri = data.getData();
    isLogPrintValue("TAG", "Uri = " + uri.toString());
    try {
        String filePath = getRealPathFromURI(mActivity, uri);
        selectedimagePath = filePath;
        String imgPath = "";
        try {
            imgPath = getRealPathFromURI(mActivity, uri);
            if (uri != null) {
                findViewById(R.id.attach_file_name).setVisibility(View.VISIBLE);
                fileNameTxtView.setText(uri.getPath());
            }
        } catch (Exception e) {
            imgPath = getFilePathForN(mActivity, uri).getPath();
            filePath = getRealPathFromURI(mActivity, uri);
            selectedimagePath = filePath;
            if (uri != null) {
                findViewById(R.id.attach_file_name).setVisibility(View.VISIBLE);
                fileNameTxtView.setText(uri.getPath());
            }
        }

        if (!imgPath.isEmpty()) {
            isLogPrintValue("UriSelectedExtra", "" + imgPath);
            String ext = imgPath.substring(imgPath.lastIndexOf(".") + 1);
            if (!ext.equals("webp") && !ext.equalsIgnoreCase("HEIC") && imgPath.lastIndexOf(".") != -1) {
                isLogPrintValue("FinalUriSelectedExtra", "" + imgPath);
                //cropImageMethod(imgPath, mActivity);
            } else {
                Toast.makeText(mActivity, mActivity.getString(R.string.str_InvalidImageType), Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(mActivity, "Failed", Toast.LENGTH_SHORT).show();
        }
    } catch (Exception e) {
        isLogPrintValue("TAG", "File select error" + e);
        try {
            String imgPat = getPath(mActivity, uri);
            isLogPrintValue("UriSelected1-imgPat", "" + imgPat);
            selectedimagePath = imgPat;
            if (uri != null) {
                findViewById(R.id.attach_file_name).setVisibility(View.VISIBLE);
                fileNameTxtView.setText(uri.getPath());
            }

        } catch (Exception es) {
            es.printStackTrace();
        }
        try {
            String imgPath = getRealPathFromURI(mActivity, uri);
            isLogPrintValue("UriSelected2-imgPath", "" + imgPath);
        } catch (Exception se) {
            se.printStackTrace();
        }
        try {
            String imgPaths = getRealPathFromUri(mActivity, uri);
            isLogPrintValue("UriSelected3-imgPaths", "" + imgPaths);
        } catch (Exception fe) {
            fe.printStackTrace();
        }
        try {
            String imgPathd = getRealPathFromUris(mActivity, uri);
            isLogPrintValue("UriSelected4_imgPathd", "" + imgPathd);
        } catch (Exception ed) {
            ed.printStackTrace();
        }
        String imagepath = getFilePathForN(mActivity, uri).getPath();
        isLogPrintValue("UriSelected5_imagepath", "" + imagepath);
        e.printStackTrace();
    }
}

Hemant Haritash

- 1 years ago

public static String getRealPathFromURI(Activity activity, Uri uri) {
    String path = "";
    if (activity.getContentResolver() != null) {
        Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            path = cursor.getString(idx);
            cursor.close();
        }
    }
    return path;
}

public static String getRealPathFromUris(Activity activity, Uri uri) {
    String[] projection = {MediaStore.Images.Media.DATA};
    CursorLoader loader = new CursorLoader(activity, uri, projection, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int Column_idx = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String result = cursor.getString(Column_idx);
    cursor.close();
    return result;
}

public static String getRealPathFromUri(Activity activity, Uri uri) {
    String filePath = "";
    Pattern p = Pattern.compile("(\\d+)$");
    Matcher m = p.matcher(uri.toString());
    if (!m.find()) {
        return filePath;
    }
    String imgId = m.group();
    String[] column = {MediaStore.Images.Media.DATA};
    String sel = MediaStore.Images.Media._ID + "=?";
    Cursor cursor = activity.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            column, sel, new String[]{imgId}, null);
    int columnIndex = cursor.getColumnIndex(column[0]);
    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }
    cursor.close();
    return filePath;
}