请,需要帮助。我有这个错误java.io.FileNotFoundException:http://news.yandex.ru/quotes/1507.png(浏览器可以看到),同时将其保存到我的内部存储中。 这是我的方法:
void downloadGraph(String link){ 试试{
URL url = new URL(link);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File dbDirectory = new File(mctx.getFilesDir().getAbsolutePath()+ File.separator+"yqimages/");
if(!dbDirectory.exists())dbDirectory.mkdir();
String fname=link.substring(link.lastIndexOf("/")+1,link.length());
File file = new File(dbDirectory, fname);
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
}
fileOutput.close();
} catch (final Exception e) {
e.printStackTrace();
}
}
你能写一个方法来下载\保存这个特定的图像(如上所示)?任何帮助表示赞赏! 得到它!!问题不在代码中,而是在图像https://news.yandex.ru/quotes/1507.png中。由于某些原因,这张图片无法保存,而其他图片则无法保存。它与“httpS://”有关吗?
答案 0 :(得分:1)
// ist put the permission in Manifest.xml hte permission are below
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
private void saveimage() {
bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
String time = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(System.currentTimeMillis());
File path = Environment.getExternalStorageDirectory();
File dir = new File(path+"/Gallery");
dir.mkdir();
String imagename = time+".PNG";
File file = new File(dir,imagename);
OutputStream out;
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
out.flush();
out.close();
Toast.makeText(Show_Online.this, "Image Save To Gallery",
Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Toast.makeText(Show_Online.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
答案 1 :(得分:0)
here解释所有关于在android中下载和保存图像的内容。
并且不要忘记在Menifest中添加读写外部存储器的权限。