80,351
社区成员
发帖
与我相关
我的任务
分享
//保存图片到sdcard
public void saveImages(String imageViews){
InputStream is = null; //定义一个输入流。
BufferedInputStream bis = null;//定义一个带缓冲的输入流 。
try{
URL url = new URL(imageViews);//创建一个URL对象。
is = url.openStream(); //打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。
bis = new BufferedInputStream(is);
}catch(Exception e){
System.out.println(e.toString());
}
//创建储存图片的文件
String midr = Environment.getExternalStorageDirectory()+ "/download";
File fird = new File(midr);
if(!fird.exists()){
fird.mkdirs(); //创建新文件夹
}
//获取系统时间
dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String systemTime = dateFormat.format(new java.util.Date());
String path = Environment.getExternalStorageDirectory()+ "/download/" + "wx"+"_"+systemTime+".jpg";
//path =mnt/xuzhong/wx_201408211452.jpg
//写到本地
BufferedOutputStream bos = null; //定义一个带缓冲的输出流。
File file = new File(path);
try{
bos = new BufferedOutputStream(new FileOutputStream(file));;
//file:mnt/sdcard/xunzhong/wx_20140821163351.jpg
byte[] b = new byte[1024]; //创建字节数组。
while(bis.read(b)!=-1){ //输入流中的数据如果还有下一行(!=-1)将继续循环
bos.write(b); //将字节数组写入输出流。
}
}catch(Exception e){
System.out.println("++++++++++++++++++"+e.toString());
}finally{
try{
bos.flush(); //刷新此缓冲的输出流。
bis.close(); //关闭此输入流 。
}catch(Exception e){
System.out.println("++++++++++++++++++"+e.toString());
}
}
}
//扫描指定文件
public void scanFileAsync(Context ctx, String filePath) {
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(Uri.fromFile(new File(filePath)));
ctx.sendBroadcast(scanIntent);
}
//扫描指定目录
public static final String ACTION_MEDIA_SCANNER_SCAN_DIR = "android.intent.action.MEDIA_SCANNER_SCAN_DIR";
public void scanDirAsync(Context ctx, String dir) {
Intent scanIntent = new Intent(ACTION_MEDIA_SCANNER_SCAN_DIR);
scanIntent.setData(Uri.fromFile(new File(dir)));
ctx.sendBroadcast(scanIntent);
}
调用这两个方法就可以了
public void scanFileAsync(Context ctx, String filePath) {
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(Uri.fromFile(new File(filePath)));
ctx.sendBroadcast(scanIntent);
}
写了一个这样的方法,把我的midr(文件目录) 丢进去,貌似还是不行