繁体   English   中英

android_从SDcard获取图片

[英]android_Get pictures from SDcard

从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);
}

在此处输入图片说明

只需尝试以下代码。

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

像这样设置路径:

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

更改+“ 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM