【问题标题】:Retrieving image using BitmapFactory - SkImageDecoder null return error使用 BitmapFactory 检索图像 - SkImageDecoder null 返回错误
【发布时间】:2014-07-14 15:06:00
【问题描述】:

我正在尝试检索我(认为)我在 Android 的片段(噩梦)中使用表面视图保存的图像。在这里,我尝试保存图像并列出目录中的所有图像。我完全不知道目录在哪里(storate/emulated/0 等)。除了像这样列出它们之外,我永远无法在任何地方找到这些图像。这表明它们可能存在?

jpegCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {


        // Create an image file name
        File image = null;
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        try {
            image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",         /* suffix */
                    storageDir      /* directory */
            );
        } catch (IOException e) {
            e.printStackTrace();
        }

        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
        Log.d("Files", "Path: " + path);
        File f = new File(path);
        File file[] = f.listFiles();
        Log.d("Files", "Size: "+ file.length);
        String filename = "";
        for (int i=0; i < file.length; i++)
        {
            Log.d("Files", "FileName "+i+" :" + file[i].getName());
            filename = file[i].getName();
        }

        Bitmap myBitmap = BitmapFactory.decodeFile(file[0].getAbsolutePath()+"/"+filename);

        ImageView taken_pic = (ImageView) view.findViewById(R.id.taken_pic);
        taken_pic.setImageBitmap(myBitmap);

    }
};

这只是给我一个图像名称列表和“SkImageDecoder::Factory returned null”消息。我可能使用文件名字符串使事情变得更糟,但即使在列表中回显的文件之一中进行硬编码,我也会得到相同的结果。图像是否应该基于此代码的结果存在?我怎样才能真正将图像再次作为位图获取?有一个更好的方法吗。我已经尝试了所有可以找到并阅读每个主题的示例。

【问题讨论】:

    标签: android surfaceview android-bitmap


    【解决方案1】:

    这是我现在工作的按钮示例,该按钮将与显示相机的表面视图一起使用,以正确保存图像(当前到下载文件夹),检索它并将其输出到图像视图(当前为“taken_pic”)。

    不确定我到底哪里出错了。我认为只是在几个小地方。一个大问题是调试困难。我的手机没有植根,所以我认为我对 Android Studios Device Monitor 的选择是有限的。通过 PC 资源管理器浏览时,您通常不会看到已保存的图像。拔掉USB,重启手机,文件应该会出现在它们应该在的位置。

    我希望这可能对某人有所帮助。

            camera_picture.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    captureImage();
                }
            });
    
            private void captureImage() {
                cam.takePicture(shutterCallback, rawCallback, jpegCallback);
            }
    
            rawCallback = new Camera.PictureCallback() {
                public void onPictureTaken(byte[] data, Camera camera) {
                    Log.d("Log", "onPictureTaken - raw");
                }
            };
    
            /** Handles data for jpeg picture */
            shutterCallback = new Camera.ShutterCallback() {
                public void onShutter() {
                    Log.i("Log", "onShutter'd");
                }
            };
    
            jpegCallback = new Camera.PictureCallback() {
                public void onPictureTaken(byte[] data, Camera camera) {
                    FileOutputStream outStream = null;
                    try {
                        File fileb = new     File(android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "imagename2.jpg");
                        outStream = new FileOutputStream(fileb);
                        outStream.write(data);
                        outStream.close();
                        Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                }
                Log.d("Log", "onPictureTaken - jpeg");
    
    
                String photoPath = android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/imagename.jpg";
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
    
                ImageView taken_pic = (ImageView) view.findViewById(R.id.taken_pic);
                taken_pic.setImageBitmap(bitmap);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      • 2012-02-29
      • 2016-03-22
      • 2017-05-10
      • 1970-01-01
      相关资源
      最近更新 更多