【问题标题】:BitmapFactory: Unable to decode stream. No such file or directoryBitmapFactory:无法解码流。没有相应的文件和目录
【发布时间】:2018-07-20 13:23:23
【问题描述】:

我目前正在使用 Camera 2 API 和 Microsoft Cognitive - Computer Vision 开发/试验“Analzye 图像应用程序”。

我没有使用普通相机,而是使用 API 来捕获图像并让计算机视觉分析位图。我在这里所做的是获取捕获图像的文件路径并使用 BitmapFactory 直接将其转换为位图。但我总是得到以下错误:

E/BitmapFactory:无法解码流:java.io.FileNotFoundException:/storage/emulated/0/IMG_20Jul2018_8112.jpg:打开失败:ENOENT(没有这样的文件或目录)

我可以在手机存储中看到图像,但位图返回 null。

这是我的代码:

onCreate里面,touchListener(双击抓图)

     textureView.setOnTouchListener(new View.OnTouchListener() {

        private GestureDetector gestureDetector = new GestureDetector(Camera.this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                Snackbar.make(findViewById(R.id.textureView), "Capturing...", Snackbar.LENGTH_SHORT).show();
                takePicture();

                //if(mBitmap == null) {
                //    mBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                //}
                // START OF COMPUTER VISION
                onActivityResult();
                // END OF COMPUTER VISION
                return super.onDoubleTap(e);
            }
            // implement here other callback methods like onFling, onScroll as necessary
        });

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            gestureDetector.onTouchEvent(event);
            return true;
        }
    });

takePicture() 函数内部(插入在 //Checkorientation base on device 之后):

        Date c = Calendar.getInstance().getTime();
        System.out.println("Current time => " + c);

        SimpleDateFormat df = new SimpleDateFormat("ddMMMyyyy");

        // Generate random number
        Random r = new Random();
        final int currentNumber = r.nextInt((9999 - 1) + 1) + 1;

        String fileName = "IMG_" + df.format(c) + "_" + currentNumber + ".jpg";
        file = new File(Environment.getExternalStorageDirectory()+"/"+fileName);

        //Convert Bitmap to stream
        try {
            Bitmap bitmap = null;
            File f= new File(pathUpload);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;

            bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);

            // Put path into bitmap
            mBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            //image.setImageBitmap(bitmap);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

        } catch (Exception e) {
            e.printStackTrace();
        }

根据错误,它处理mBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

可能是什么错误?

请在此处建立代码: Camera 2 APIMicrosoft Computer Vision

提前谢谢你们!

编辑:附加信息

我已设置用户权限以使用相机和访问存储。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

另外,我在运行时请求了许可。请参考here

【问题讨论】:

  • 您是否有权读取该文件?您是否在运行时请求读取权限(不仅仅是在您的清单中)?
  • @GabeSechan 是的,先生。我提到了这个链接。 link
  • 请使用 File.seperator 而不是“/”。你能检查一下 file.exists() 返回什么吗?在调用解码文件之前,你应该添加 do if(file.exists())
  • @oiyio 相同的错误输出。我试过file.exists 和它的空值。
  • null 什么? file.exists() 返回 false 或 true。

标签: android microsoft-cognitive


【解决方案1】:

在调用 BitmapFactory.decodeFile 之前检查文件是否存在。

还要确保您具有读/写权限。在清单文件中写入以下内容。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

出于写入目的,您还应该在运行时为 api >=23 授予权限。

【讨论】:

  • 回答 Gabe Sechan 的问题。是的,我拥有运行时和清单的权限
  • 好的,然后检查某个路径和某个文件,例如手动将文件放入图片文件夹并测试它是否有效
【解决方案2】:

检查你的路径,因为 decodeFile() 需要一个文件系统路径。这样的事情是行不通的。

file:///storage/emulated/0/camera1/pictureblablabla.jpg

你需要类似的东西

/storage/emulated/0/camera1/pictureblablabla.jpg

【讨论】:

  • 正如错误所说,它只是/storage/emulated/0/IMG_20Jul2018_8716.jpg。所以我假设路径没有 file://
猜你喜欢
  • 2019-11-11
  • 1970-01-01
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 2015-05-23
相关资源
最近更新 更多