Android 图片的加载与保存

来源:互联网 发布:阿里云 ftp 安全规则 编辑:程序博客网 时间:2024/06/01 13:54

从手机中加载图片


  1. File file = new File("/data/data/capture.bmp");  
  2.        if(file.exists()){        //判断文件是否存在  
  3.                    bm = BitmapFactory.decodeFile("/data/data/capture.bmp");//通过BitmapFactory将图片文件转成Bitmap  
  4.                 //  iv.setImageBitmap(bm);//使用ImageView 的setImageBitmap()显示图片  
  5.          
  6.    }else{         
  7.     //文件不存在  
  8.        new AlertDialog.Builder(Image.this).setIcon(R.drawable.icon)  
  9.        .setTitle("文件不存在").setPositiveButton("ok",new DialogInterface.OnClickListener(){  
  10.            public void onClick(DialogInterface dialog, int which) {}  
  11.      }).show();  
  12.     }  
.保存图片到手机中

  1. File f = new File("/data/data/1.png");  
  2.                     f.createNewFile();  
  3.                     FileOutputStream fOut = null;  
  4.                     try {  
  5.                             fOut = new FileOutputStream(f);  
  6.                     } catch (FileNotFoundException e) {  
  7.                             e.printStackTrace();  
  8.                     }  
  9.                     bmp1.compress(Bitmap.CompressFormat.PNG, 100, fOut);  
  10.                     try {  
  11.                             fOut.flush();  
  12.                     } catch (IOException e) {  
  13.                             e.printStackTrace();  
  14.                     }  
  15.                     try {  
  16.                             fOut.close();  
  17.                     } catch (IOException e) {  
  18.                             e.printStackTrace();  
  19.                     }  

原创粉丝点击