-3

I have a String with an url to an image.

Can somebody give me the code to realize it?

All what ive done before is not working.

code so far:

 dlbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new DownloadImage().execute(id);
            }
        });

  public void saveImage(Context context, Bitmap b, String imageName)
    {
        FileOutputStream foStream;
        try
        {
            foStream = context.openFileOutput(imageName, Context.MODE_PRIVATE);
            b.compress(Bitmap.CompressFormat.PNG, 100, foStream);
            foStream.close();
        }
        catch (Exception e)
        {
            Log.d("saveImage", "Exception 2, Something went wrong!");
            e.printStackTrace();
        }
    }



    private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
        private String TAG = "DownloadImage";
        private Bitmap downloadImageBitmap(String sUrl) {
            Bitmap bitmap = null;
            try {
                InputStream inputStream = new URL(sUrl).openStream();   // Download Image from URL
                bitmap = BitmapFactory.decodeStream(inputStream);       // Decode Bitmap
                inputStream.close();
            } catch (Exception e) {
                Log.d(TAG, "Exception 1, Something went wrong!");
                e.printStackTrace();
            }
            return bitmap;
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            return downloadImageBitmap(params[0]);
        }

        protected void onPostExecute(Bitmap result) {
            saveImage(getApplicationContext(), result, "my_image.png");
        }
    }

public Bitmap loadImageBitmap(Context context, String imageName) { Bitmap bitmap = null; FileInputStream fiStream; try { fiStream = context.openFileInput(imageName); bitmap = BitmapFactory.decodeStream(fiStream); fiStream.close(); } catch (Exception e) { Log.d("saveImage", "Exception 3, Something went wrong!"); e.printStackTrace(); } return bitmap; }

but nothing happens. no stored image in gallery and no toast will be shown

1 Answers1

-1

Use Picasso Library for this task

http://square.github.io/picasso/

qasim butt
  • 133
  • 11