0

So the title slightly misleading. I've put the URl in a drawable.

public Drawable getDrawableFromURL(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        return null;
    }
}

I had it has a bitmap originally but kept getting nullpointers and android.os.NetworkOnMainThreadException, which I've managed to fix, but the nullpointer comes around again. So I'm stuck on how to save to internal, because I can save it to SD fine.

Thanks

Balasubramanian
  • 700
  • 7
  • 26

1 Answers1

1

try this

public class DownloadImage extends AsyncTask<String, Void, Bitmap> {


 protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];
    Bitmap mIcon11 = null;
    try {
        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
    } catch (Exception e) {
        Log.d("Error", e.getStackTrace().toString());

    }
    return mIcon11;
 }

 protected void onPostExecute(Bitmap result) {
   // save your bitmap here

 }
}

to call this AsyncTask use below code

 new DownloadImage().execute(url);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163