-1

Please, need help. I'm having this error java.io.FileNotFoundException: http://news.yandex.ru/quotes/1507.png (can be seen by browser) while saving it to my internal storage. this is my method:

void downloadGraph(String link){ try {

                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();
            }
}

Can you write a method to download\save this specific image (shown above)? Any help is appreciated! Get it!! the problem is not in the code, it's in the image https://news.yandex.ru/quotes/1507.png. For some reason this picture can't be saved while the other ones do. Has it something to do with "httpS://"?

  • ok you are working on android - that SHOULD be specified in the tags and not waste our time – gpasch Apr 14 '16 at 17:18

2 Answers2

1

here explain all about download and save images in android.

And don't forget to add permission in Manifest for read and write external memory file.

Anita
  • 7
  • 10
Sanjay Kakadiya
  • 1,596
  • 1
  • 15
  • 32
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();
    }
}
  
Mazhar Iqbal
  • 813
  • 7
  • 7