Android中的图像路径返回null

时间:2017-08-31 10:59:59

标签: android

我想获取图片路径并上传到服务器。 在这里,我成功地从图库中读取图像并设置为图像视图,但图像路径返回null。

public void onActivityResult(int requestCode, int resultCode, Intent data)
            if (resultCode == RESULT_OK) {
                if (requestCode == SELECT_PICTURE) {
                    // Get the url from data
                    Uri selectedImageUri = data.getData();
                    if (null != selectedImageUri) {
                        // Get the path from the Uri
                        String path = getRealPathFromURI(getActivity(), selectedImageUri);
                        Log.i(TAG, "IMAGE" + path);
                        Log.d("INFO", selectedImageUri.toString());
                        // Set the image in ImageView
                        profilepicture.setImageURI(selectedImageUri);
                    }
                }
            }
        }

        /* Get the real path from the URI */
        public String getRealPathFromURI(Context context, Uri contentUri) {
            Cursor cursor = null;
            try {
                String[] proj = {MediaStore.Images.Media.DATA};
                cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        }

4 个答案:

答案 0 :(得分:0)

删除getRealPathFromURI(),因为它无法可靠地运作。

相反,请使用您喜欢的图片加载库(例如Picasso,Glide)来加载Uri中的图片。

或者,在最糟糕的情况下,使用getContentResolver().openInputStream()获取InputStream标识的内容Uri,然后将该流传递给BitmapFactory.decodeStream()。请在后台线程上执行此I / O(哪些图像加载库将为您处理,以及其他好处)。

答案 1 :(得分:0)

更新方法getRealPathFromURI:

public String getRealPathFromURI(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = {MediaStore.Images.Media.DATA};
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String imagePath = cursor.getString(column_index);
            if (imagePath == null) {
                File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES), "New");
                if (!mediaStorageDir.exists()) {
                    if (!mediaStorageDir.mkdirs()) {
                        Log.d("MyCameraApp", "failed to create directory");
                        return null;
                    }
                }
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                File file;
                String path = "img_" + timeStamp + ".jpg";
                file = new File(mediaStorageDir.getPath() + File.separator + path);

                imagePath = file.getAbsolutePath();
                ParcelFileDescriptor parcelFileDescriptor = null;
                try {
                    parcelFileDescriptor = context.getContentResolver()
                            .openFileDescriptor(contentUri, "r");
                    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                    Bitmap image = BitmapFactory
                            .decodeFileDescriptor(fileDescriptor);
                    parcelFileDescriptor.close();
                    saveBitmapToPath(image, imagePath);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return imagePath;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

答案 2 :(得分:-1)

试试这个

String path = yourAndroidURI.toString() // "/mnt/sdcard/FileName.mp3"
File file = new File(new URI(path));

答案 3 :(得分:-1)

使用此功能将字符串URL转换为位图

public Bitmap getImage(String url) {
        try {
            BufferedInputStream bis = new BufferedInputStream(new 
            URL(url).openStream(), BUFFER_IO_SIZE);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(baos, 
            BUFFER_IO_SIZE);
            copy(bis, bos);
            bos.flush();
            return BitmapFactory.decodeByteArray(baos.toByteArray(), 0, 
            baos.size());
        } catch (IOException e) {
            Log.d(TAG, "loadImageFromArrayList: IMAGE DOWNLOAD FAILED!" +e);
        }
        return null;
    }