【问题标题】:Can not decode bitmap Image returns null无法解码位图图像返回 null
【发布时间】:2012-10-05 05:40:50
【问题描述】:

我有点击图片并将其保存在 sd 卡上的相机应用程序 当我解码它给出空位图时,这里是代码

Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"FacePhoto.jpg");

我已经在 ddms 中检查了 sd 卡上存在 Facephoto 文件

我得到“空”位图可能是什么问题

【问题讨论】:

    标签: android bitmap


    【解决方案1】:

    你少了一个斜线,改代码如下。

    Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/FacePhoto.jpg"); 
    

    【讨论】:

      【解决方案2】:

      你的图片路径可能有问题,这样检查

      File rootDir = Environment.getExternalStorageDirectory();
      File file = new File(rootDir + "/FacePhoto.jpg");
      
      if (file.exists())
      {
          Bitmap bitmap = BitmapFactory.decodeFile(file);
      
      }
      else
      {
         System.out.println("File Not Exists. Check the path!!");
      }
      

      【讨论】:

        【解决方案3】:

        尝试使用

        Environment.getExternalStorageDirectory()+"/FacePhoto.jpg"

        使用“/文件名”而不是“文件名”。

        如果这不起作用,请尝试使用

        Environment.getExternalStorageDirectory().getAbsolutePath()+"/FacePhoto.jpg"

        需要注意的一点是使用绝对路径而不是相对路径。

        还要查看代码中的文件名是否拼写正确。

        【讨论】:

          【解决方案4】:

          现在你必须像这样使用活动结果。

          if (requestCode == CAMERA) {
                          final File file = getTempFile();
          
                          new Thread(new Runnable() {
          
                              @Override
                              public void run() {
                                  try {
                                      selPath = file.getAbsolutePath();
                                      final String selectedImagePath = file
                                              .getAbsolutePath();
                                      bitmap = BitmapFactory
                                              .decodeFile(selectedImagePath);
                                      selPath = selectedImagePath;
          
                                  } catch (Exception e) {
                                      Log.v(TAG, "Exception: " + e.toString());
                                      handler.sendEmptyMessage(IMAGENOTLOADED);
                                  }
                              }
                          }).start();
                      }
          

          getTempFile 方法是这样的

          private File getTempFile() {
              final File path = new File(Environment.getExternalStorageDirectory(),
                      getPackageName());
              if (!path.exists()) {
                  path.mkdir();
              }
              return new File(path, fileName);
          }
          

          【讨论】:

            【解决方案5】:

            尝试以下方法:

                 FileInputStream instream = new FileInputStream("/sdcard/Pics/Image.png"); 
                        BufferedInputStream bif = new BufferedInputStream(instream); 
                        byteImage1 = new byte[bif.available()]; 
                        bif.read(byteImage1); 
                         BitmapFactory.decodeByteArray(byteImage1, 0, byteImage1.length);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2020-05-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-08-13
              • 1970-01-01
              • 2018-03-12
              相关资源
              最近更新 更多