2013-05-21 41 views
1

我尝试在imageView中显示我创建的png文件。当我启动我的程序并点击showCreatedPng按钮时,模拟器屏幕上只有一个白色的空白页面。我的模拟器有200 MB的SD卡,但是,我找不到我的电脑中创建的PNG的位置。当我启动程序时,我的电话,PNG保存了在GT-I9100 \电话文件夹中。我猜的文件夹是一个内部文件夹中。无论如何,我看不到我创建PNG文件。请帮助。)Android从内部/外部存储获取图像

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

您正试图打开一个名为*.png的文件。这也许应该是QRCode.png,使得码

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

Thaks很多:D有时我看不到像这样的小东西。 :d –

-1
 Use the adb tool with push option to copy test2.png onto the sdcard. 

    bash-3.1$ /usr/local/android-sdk-linux/tools/adb push test2.png /sdcard/ 


    This is the easiest way to load bitmaps from the sdcard. Simply pass the path to the image to BitmapFactory.decodeFile() and let the Android SDK do the rest. 

    package higherpass.TestImages; 

    import android.app.Activity; 
    import android.graphics.Bitmap; 
    import android.graphics.BitmapFactory; 
    import android.os.Bundle; 
    import android.widget.ImageView; 

    public class TestImages extends Activity { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      ImageView image = (ImageView) findViewById(R.id.test_image); 
      Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png"); 
      image.setImageBitmap(bMap); 
     } 
    } 

All this code does is load the image test2.png that we previously copied to the 
sdcard. The BitmapFactory creates a bitmap object with this image 

,我们使用ImageView.setImageBitmap()方法来更新 ImageView的组件。

相关问题