android - 从uri获取实际路径?

标签 android

这是我用来获取图片 uri 的代码 Uri imageUri = data.getData();

如何获取所选图像的实际路径?

我目前得到的Uri值/路径是

content://com.miui.gallery.open/raw/%2Fstorage%2Femulated%2F0%2FLightStick%2F144pixels.bmp

我需要的图像的正确文件路径是我的其他功能

/storage/emulated/0/LightStick/144pixels.bmp

图片选择函数:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT_OK){//everything processed successfully
        if(requestCode == IMAGE_GALLERY_REQUEST){ //hearing back from image gallery

            //the address of the image on the SD card
            Uri imageUri = data.getData();
            BMPfilepath =imageUri.getPath();
            //stream to read image data from SD card
            InputStream inputStream;

            try {
                inputStream = getContentResolver().openInputStream(imageUri);//getting an input stream, based no the URI of image
                Bitmap image = BitmapFactory.decodeStream(inputStream);//get bitmap from stream
                imgPicture.setImageBitmap(image);//show image to user in imageview

            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(this, "Unable to open image", Toast.LENGTH_LONG).show(); //let user know image unavail
            }//catch
        } //requestCode == IMAGE_GALLERY_REQUEST
    }

使用上一个函数中的 imageUri 的上传函数。 String path = imageUri.getPath().toString(); 应用程序在调试时崩溃并转到循环文件

public void onUploadToArduino(){
    String path = imageUri.getPath().toString();//<- app crashes and goes to a looper file when in debug
    String sdpath = System.getenv("EXTERNAL_STORAGE");
    String extStore = Environment.getExternalStorageDirectory().getPath();
    String FILENAME = extStore + "/LightStick/144pixels.bmp";

    String collected = null;
    FileInputStream fis = null;

    FileInputStream fileInputStream = null;
    byte[] bytesArray = null;

    try {
        File file = new File(FILENAME);//<- this works
        //File file = new File(path);//<- this doesnt
        bytesArray = new byte[(int) file.length()];

        //read file into bytes[]
        fileInputStream = new FileInputStream(file);
        fileInputStream.read(bytesArray);

最佳答案

How do I get the actual path of an image selected?

你不知道。 Uri 不是文件,可能不指向文件,更不用说您可以访问的文件了。

使用 ContentResolveropenInputStream() 获取由 Uri 标识的内容的 InputStream。理想情况下,只需使用流。如果您需要一个 File 用于其他一些编写不当且不支持流的 API:

  • 在您控制的某些 File 上创建一个 FileOutputStream(例如,在 getCacheDir() 中)

    <
  • 使用 InputStreamFileOutputStream 将字节复制到您的文件

  • 使用你的文件

关于android - 从uri获取实际路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46754817/

相关文章:

android - 无法播放此视频 VideoView

android - 无法从命令提示符将 android apk 安装到模拟器

android - bluetoothGatt writeCharacteristic 返回 false

android - 如何使 tablayout 文本大小相等?

android - android中的持久服务/线程

android - 在 onClick(DialogInterface v, int buttonId) 中获取上下文?

java - android 7 : NullPointerException on APK installing with Runtime. getRuntime().exec

javascript - 如何缩放 block 元素以在不同的移动屏幕上显示相同的大小?

android - 编写一个可以跨设备更新的应用程序

屏幕上的 Android ViewPager 中心项目