有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 安卓_从SD卡获取图片

拍照​​SD卡的路径。 我的照片名是01。jpg

确保图片在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


共 (3) 个答案

  1. # 1 楼答案

    试试下面的代码

    File f = new File("/mnt/sdcard/01.jpg");
    ImageView mImgView1 = (ImageView)findViewById(R.id.imageView);
    Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
    mImgView1.setImageBitmap(bmp);
    
  2. # 2 楼答案

    更改+“sdcard/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);
    }
    
  3. # 3 楼答案

    按如下方式设置路径:

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