Jam*_*mie 13 java maps android emulation
我想将图像从URL保存到SD卡(以备将来使用),然后从SD卡加载该图像,将其用作Google地图的可绘制叠加层.
这是函数的保存部分:
//SAVE TO FILE
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
String extraPath = "/Map-"+RowNumber+"-"+ColNumber+".png";
filepath += extraPath;
FileOutputStream fos = null;
fos = new FileOutputStream(filepath);
bmImg.compress(CompressFormat.PNG, 75, fos);
//LOAD IMAGE FROM FILE
Drawable d = Drawable.createFromPath(filepath);
return d;
Run Code Online (Sandbox Code Playgroud)
图像以succuessfully的形式保存到SD卡,但在到达createFromPath()
线路时失败.我不明白为什么它会保存到那个目的地但不加载它....
Gir*_*ran 28
试试这个代码.它有效......
try
{
URL url = new URL("Enter the URL to be downloaded");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory().getAbsoluteFile();
String filename="downloadedFile.png";
Log.i("Local filename:",""+filename);
File file = new File(SDCardRoot,filename);
if(file.createNewFile())
{
file.createNewFile();
}
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 )
{
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
}
fileOutput.close();
if(downloadedSize==totalSize) filepath=file.getPath();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
filepath=null;
e.printStackTrace();
}
Log.i("filepath:"," "+filepath) ;
return filepath;
Run Code Online (Sandbox Code Playgroud)
Anh*_*arp 27
DownloadManager为您完成所有这些工作.
public void downloadFile(String uRl) {
File direct = new File(Environment.getExternalStorageDirectory()
+ "/AnhsirkDasarp");
if (!direct.exists()) {
direct.mkdirs();
}
DownloadManager mgr = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(uRl);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir("/AnhsirkDasarpFiles", "fileName.jpg");
mgr.enqueue(request);
// Open Download Manager to view File progress
Toast.makeText(getActivity(), "Downloading...",Toast.LENGTH_LONG).show();
startActivity(Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
}
Run Code Online (Sandbox Code Playgroud)
尝试使用此代码将图像从URL保存到SDCard.
URL url = new URL ("file://some/path/anImage.png");
InputStream input = url.openStream();
try {
File storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream (storagePath, "myImage.png");
try {
byte[] buffer = new byte[aReasonableSize];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
}
finally {
output.close();
}
}
finally {
input.close();
}
Run Code Online (Sandbox Code Playgroud)
如果要在SD卡上创建子目录,请使用:
File storagePath = new File(Environment.getExternalStorageDirectory(),"Wallpaper");
storagePath.mkdirs();
Run Code Online (Sandbox Code Playgroud)
创建子目录"/ sdcard/Wallpaper /".
希望它会对你有所帮助.
请享用.:)
归档时间: |
|
查看次数: |
47112 次 |
最近记录: |