1
A
回答
0
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdcard.getAbsolutePath() + "/dir1/dir2");
dir.mkdirs();
File file = new File(dir, "filename");
FileOutputStream f = new FileOutputStream(file);
不要忘了添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
0
private final String PATH = "/data/data/com.whatever.whatever/"; //put the downloaded file here
public void DownloadFromUrl(String imageURL, String fileName) { //this is the downloader method
try {
URL url = new URL("http://yoursite.com/" + imageURL); //you can write here any link
File file = new File(fileName);
long startTime = System.currentTimeMillis();
Log.d("ImageManager", "download begining");
Log.d("ImageManager", "download url:" + url);
Log.d("ImageManager", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
Log.d("ImageManager", "download ready in"
+ ((System.currentTimeMillis() - startTime)/1000)
+ " sec");
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
}
而且不要忘记添加以下权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
0
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
try {
// Make sure the Pictures directory exists.
path.mkdirs();
// Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not
// try to copy it in chunks). Note that if external storage is
// not currently mounted this will silently fail.
InputStream is = //Input stream of the file downloaded;
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
1
试试这个代码:
File src = new File(Your_current_file);
File dest = new File(destination_place);
public void copyFile(File src, File dest) throws IOException
{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
out.close();
}
确保提供用于外部存储设备的权限,如果你需要:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
希望这将帮助你。
相关问题
- 1. android - 如何将图像复制到SD卡中的文件夹
- 2. android: - 如何将图像从资产复制到SD卡?
- 3. 如何将图像复制到SD卡上的现有目录?
- 4. Android将图像保存在SD卡中
- 5. 如何将图像从硬盘复制到Android模拟器的SD卡?
- 6. 如何将SD卡上的PNG图像加载到Canvas以在Android中绘制?
- 7. 在Android中将位图图像存储到SD卡中
- 8. 从SD卡复制图像到Android项目包
- 9. 复制图像从assest到sd卡安装android应用程序
- 10. 如何将下载的图像存储到Android SD卡
- 11. 如何将图像保存到SD卡从Url android
- 12. Android:如何从SD卡上传图像
- 13. Android:如何:从SD卡显示图像?
- 14. Android:复制资产安装到SD卡
- 15. 在Android中将图像保存到SD卡
- 16. Android-如何使用SD卡中的图像集制作视频?
- 17. 保存图像并复制SD卡中的所有图像
- 18. MonoDroid:将资产复制到SD卡
- 19. 将文件复制到SD卡
- 20. Android的图像保存到SD卡
- 21. 如何添加SD卡图像到coverflow?
- 22. 将图像存储到SD卡?
- 23. 将图像保存到SD卡
- 24. 如何将android中的SD卡中的图像存储到数据库中?
- 25. 如何从SD卡中顺利显示图像在Android中
- 26. android viewflow从SD卡刷卡图像
- 27. 如何在android/ANDROID中将图片从SD卡分享给Gmail?
- 28. 如何在使用Fresco时将图像保存到SD卡?
- 29. 如何将res/raw文件夹中的xml文件复制到android的sd卡?
- 30. 将位图保存到SD卡Android
您应该搜索1st:http://blogingtutorials.blogspot.com/2010/12/download-image-on-sdcard-from-url-in.html – Arslan