【问题标题】:Android get image from internal/external storageAndroid 从内部/外部存储中获取图像
【发布时间】:2013-05-21 22:54:58
【问题描述】:

我尝试在 imageView 中显示我创建的 png 文件。当我启动程序并单击 showCreatedPng 按钮时,模拟器屏幕上只有一个白色的空白页。

并且 logCat 说:(可能 Logcat 显示错误是因为模拟器。我的模拟器有 200 mb sd-card 但是,我无法在我的电脑中找到创建的 PNG。当我启动我的手机程序时,PNG 保存在GT-I9100\Phone 文件夹。我猜这个文件夹是一个内部文件夹。反正我创建的 png 文件我看不到。请帮助。)

05-21 21:53:39.764: E/BitmapFactory(1335): Unable to decode stream: java.io.FileNotFoundException: /mnt/sdcard/*.png: open failed: ENOENT (No such file or directory)

这些是我使用的代码。

为了节省:

private void saveImageToExternalStorage(Bitmap bitmap) {
    String qrPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
    try
    {
       File dir = new File(qrPath);
       if (!dir.exists()) {
          dir.mkdirs();
       }
       OutputStream fOut = null;
       File file = new File(qrPath, "QRCode.png");
       if(file.exists()){
          file.delete();
          file.createNewFile();
          fOut = new FileOutputStream(file);
          bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
          fOut.flush();
          fOut.close();
       }
    }
    catch (Exception e) {
      Log.e("saveToExternalStorage()", e.getMessage());
    }
}

以及获取 png 文件的代码:

 String qrPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/*.png";
 BitmapFactory.Options options = new BitmapFactory.Options();
 options.inPreferredConfig = Bitmap.Config.ARGB_8888;
 Bitmap mustOpen = BitmapFactory.decodeFile(qrPath, options);

 setContentView(R.layout.qr_image);
 ImageView imageView = (ImageView) findViewById(R.id.qr_image);
 imageView.setImageBitmap(mustOpen);

感谢您的帮助。

【问题讨论】:

    标签: android image imageview bitmapfactory


    【解决方案1】:

    您正试图打开一个名为 *.png 的文件。这应该是QRCode.png,因此代码

    Environment.getExternalStorageDirectory().getAbsolutePath() + "/QRCode.png";
    

    【讨论】:

    • 非常感谢 :D 有时我看不到这样的小事。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    相关资源
    最近更新 更多