我想从网址下载图片并保存到内部存储空间 我有这个代码,但如果我的手机没有外置存储,则无法下载图像
Bitmap b=null;
URL url;
InputStream iStream=null;
File tmpFile;
try {
url = new URL(strURL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
iStream = urlConnection.getInputStream();
File cacheDirectory = context.getCacheDir();
tmpFile = new File(cacheDirectory.getPath() + "/news.png");
FileOutputStream fOutStream = new FileOutputStream(tmpFile);
b = BitmapFactory.decodeStream(iStream);
b.compress(Bitmap.CompressFormat.PNG,100, fOutStream);
fOutStream.flush();
fOutStream.close();
}
catch(Exception ee)
{
}
return b;
您是否帮我将图片从网址下载到外部存储的内部存储空间
答案 0 :(得分:0)
首先需要检查手机是否有外部目录。 android documentation已经有代码可以执行此操作。
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}