您的位置:首页 > 理论基础 > 计算机网络

【Android】网络下载图片&SD卡文件存储

2012-09-19 23:44 429 查看
(本博客的作用:可以使用源码在虚拟机里面的SD卡上添加图片(找到URL即可),可以用来操作SD卡上的文件)

以下是一个从网络下载图片的函数,放入URL即可:

public Bitmap returnBitMap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
if(bitmap == null){
Log.d("RRRyy", "Null");
}

return bitmap;
}


比如传入:"http://pica.nipic.com/2008-04-01/200841194113617_2.jpg",返回的是bitmap

下面是一个传入bitmap和文件名(文件名要有扩展名),将图片存入SD卡的类:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;

/**
* 保存图片的类
*
* @author Administrator
*
*/
public class PicSave {

private final static String CACHE = "/Caochen";//存储的文件夹

/**
* 保存图片的方法
* 保存到sdcard
* @throws IOException
*/
public void savePic(Bitmap b, String strFileName) {                 String filePath = isExistsFilePath();
FileOutputStream fos = null;//流
File file = new File(filePath, strFileName);
if (file.exists()) {//如果存在,未处理
} else {
try {
fos = new FileOutputStream(file);
if (null != fos) {
b.compress(Bitmap.CompressFormat.JPEG, 90, fos);//格式
fos.flush();
fos.close();
Log.d("YYHU", "ok");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 获取sd卡的缓存路径,
* 一般在卡中sdCard就是这个目录
*
* @return SDPath
*/
public static String getSDPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();// 获取根目录
} else {
Log.e("ERROR", "没有内存卡");
}
return sdDir.toString();
}

/**
* 获取缓存文件夹目录 如果不存在创建 否则则创建文件夹
*
* @return filePath
*/
private String isExistsFilePath() {
String filePath = getSDPath()+CACHE;
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();//建立文件夹
}
return filePath;
}
}


记住,为了正确运行上面两个类,一定要添加下面的权限:

<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>


为了更加清楚的知道SD卡文件夹的操作,我在源代码中加入如下输出:

/**
* 获取sd卡的缓存路径,
* 一般在卡中sdCard就是这个目录
*
* @return SDPath
*/
public static String getSDPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();// 获取根目录
} else {
Log.e("ERROR", "没有内存卡");
}
Log.d("a", sdDir.getAbsolutePath());//  /mnt/sdcard
Log.d("b", sdDir.toString());//      /mnt/sdcard
Log.d("c", sdDir.getPath());//       /mnt/sdcard
Log.d("d", sdDir.getParent());//      /mnt
Log.d("e", sdDir.getName());//       sdcard                 Log.d("g", sdDir.toURI() + "");//     file:/mnt/sdcard
return sdDir.toString();

}


输出如注释所示!

下面为了更好的理解,贴出在SD卡中寻找所有图片的代码:输入为根目录,比如:/mnt/sdcard

import java.io.File;
import java.util.ArrayList;

import android.util.Log;

public class PicDir {

public void getFileDir(String opath){
File f = new File(opath);
File[] files = f.listFiles();
if (files != null) {
int count = files.length;
for (int i = 0; i < count; i++){
File file = files[i];
String filepath = file.getAbsolutePath();
String path = file.getPath();

if (filepath.endsWith("jpg") || filepath.endsWith("gif")|| filepath.endsWith("bmp")
|| filepath.endsWith("png")) {

ArrayList<String> myFilePath=new ArrayList<String>();
myFilePath.add(filepath);//这里每次扫描目录都会添加。

for (String ll : myFilePath) {
Log.e("myFilePath", ll.toString());
}
}
else{ // 目标为文件夹,进入该文件夹继续遍历
if (file.isDirectory()) {
this.getFileDir(path);
}
}
continue;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐