Android从内部/外部存储获取图像

时间:2013-05-21 22:54:58

标签: android image imageview bitmapfactory

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

并且logCat说:(也许Logcat因为模拟器而显示错误。我的模拟器有200 mb的SD卡但是,我找不到计算机中创建的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);

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

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

Environment.getExternalStorageDirectory().getAbsolutePath() + "/QRCode.png";
相关问题