2014-09-13 74 views
0

从SDcard获取图片路径。 我的照片名称是01.jpg。android_从SDcard获取图片

确保图片在SD卡内。

public boolean fileIsExists(){ 
    try{ 
     File f1 = new File("/sdcard/01.jpg"); 
     f1 = new File("01.jpg"); 
     if(!f1.exists()){ 
      return true; 
     } 
    }catch (Exception e) { 
     // TODO: handle exception 
     return false; 
    } 
    return true; 
} 
boolean file1 = fileIsExists(); 

如果图片在SD卡内,则将图片放入imageview。

错误是在下面的代码

if (file1==true){ 
    imageView = (ImageView)findViewById(R.id.image_view); 

    String myJpgPath = "/sdcard/01.jpg"; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 0; 
    Bitmap bitmap = BitmapFactory.decodeFile(myJpgPath, options);//error here Cellphone can't run 
    imageView.setImageBitmap(bitmap); 
} 

enter image description here

+0

哪种类型的错误得到些什么? – ckpatel 2014-09-13 06:34:44

+0

从手机:此应用程序已停止工作。 – Chunhao 2014-09-13 06:46:15

+1

String imageInSD = Environment.getExternalStorageDirectory()。getAbsolutePath()+“/ 01.jpg”; 位图位图= BitmapFactory.decodeFile(imageInSD); ImageView myImageView =(ImageView)findViewById(R.id.imageview); myImageView.setImageBitmap(bitmap); – ckpatel 2014-09-13 06:50:04

回答

0

更改+ “SD卡/ 01.JPG”;到+“/ 01.jpg”;

(并确保位图位图是不是重复的局部变量。)

if (file1==true){ 
    String myJpgPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/01.jpg"; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 0; 
    Bitmap bitmap = BitmapFactory.decodeFile(myJpgPath, options); 
    imageView.setImageBitmap(bitmap); 
} 
0

试试看下面的代码。

File f = new File("/mnt/sdcard/01.jpg"); 
ImageView mImgView1 = (ImageView)findViewById(R.id.imageView); 
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath()); 
mImgView1.setImageBitmap(bmp); 
+0

但我的其他应用程序可以运行没有任何错误。只有这个程序提醒错误。 – Chunhao 2014-09-13 06:49:31

0

设置你这样的路径:

String myJpgPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"folderName/01.jpg"; 
+0

但我的其他应用程序可以运行没有任何错误。只有这个程序提醒错误。 – Chunhao 2014-09-13 06:48:56