我正在使用此方法从wallpaperURLStr
下载图片:
private class DownloadWallpaperTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String wallpaperURLStr = params[0];
String localFileName = Integer.toString(wallpaperURLStr.hashCode());
try {
File cacheDir = GlobalClass.instance().getCacheFolder(MainActivity.this);
File cacheFile = new File(cacheDir, localFileName);
if (!cacheFile.exists()) {
URL wallpaperURL = new URL(wallpaperURLStr);
URLConnection connection = wallpaperURL.openConnection();
//get file length
int filesize = connection.getContentLength();
if (filesize < 0) {
downloadProgressDialog.setMax(1000000);
} else {
downloadProgressDialog.setMax(filesize);
}
InputStream inputStream = new BufferedInputStream(wallpaperURL.openStream(), 10240);
FileOutputStream outputStream = new FileOutputStream(cacheFile);
byte buffer[] = new byte[1024];
int dataSize;
int loadedSize = 0;
while ((dataSize = inputStream.read(buffer)) != -1) {
loadedSize += dataSize;
publishProgress(loadedSize);
outputStream.write(buffer, 0, dataSize);
}
outputStream.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return localFileName;
}
protected void onProgressUpdate(Integer... progress) {
downloadProgressDialog.setProgress(progress[0]);
}
protected void onPostExecute(String result) {
downloadProgressDialog.hide();
//open preview activity
Bundle postInfo = new Bundle();
postInfo.putString("localpath", result);
if (previewPanelIntent == null) {
previewPanelIntent = new Intent(MainActivity.this,
PreviewPanel.class);
}
previewPanelIntent.putExtras(postInfo);
startActivity(previewPanelIntent);
}
}
并将此方法保存在内部目录中:
public class GlobalClass {
private static GlobalClass instance;
private static final String applicationCacheFolder = "wallpaper_jms/cache/";
private static final String applicationPicFolder = "wallpaper_jms/data/";
public static GlobalClass instance() {
if (instance == null) {
instance = new GlobalClass();
}
return instance;
}
public File getCacheFolder(Context context) {
File cacheDir = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
cacheDir = new File(Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/");
if(!cacheDir.isDirectory()) {
cacheDir.mkdirs();
}
}
if(!cacheDir.isDirectory()) {
cacheDir = context.getCacheDir(); //get system cache folder
}
return cacheDir;
}
public File getDataFolder(Context context) {
File dataDir = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
dataDir = new File(Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/");
if(!dataDir.isDirectory()) {
dataDir.mkdirs();
}
}
if(!dataDir.isDirectory()) {
dataDir = context.getFilesDir();
}
return dataDir;
}
}
我可以下载图片,但我无法将其保存在图库中。
这段代码出了什么问题?
提前致谢。
答案 0 :(得分:1)
您需要将其广播到MediaScanner
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));