1

I am trying to convert a Drawable to a bitmap so I can save it to my external SD card on my Android Device.

I am using the following things to get the Drawable image:

public void setImage(Drawable image) {
    ImageView filmCover =  (ImageView)findViewById(R.id.filmCover);
    filmCover.setImageDrawable(image);

    filmCover.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            }
    });
}

This Drawable image is coming from another class:

package com.example.devopdracht;

import java.io.InputStream;
import java.net.URL;

import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;

public class PlaatjeLoader extends AsyncTask {
    static Datasaver saver = new Datasaver();
    private Drawable image;
    private Filmpagina main;

    public PlaatjeLoader(Filmpagina main) {
        this.main = main;
    }

    @Override
    protected Object doInBackground(Object... arg0) {
        JSONtester filmjson = new JSONtester();
        FilmObject film = filmjson.getFilmObject(saver.getClickedFilm());
        String cover = film.getFilmCover();
        image = LoadImageFromWebOperations("http://www.stoux.nl/film/covers/" + cover);
        return true;
    }

    @Override
    protected void onPostExecute(Object result) {
        super.onPostExecute(result);
        main.setImage(image);
    }

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

A method I spotted on Stackoverflow was:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.something);
MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "testname" , "testdescription");

Yet this doesn't work since I'm not calling a R.drawable.something but rather just image, which is of course a Drawable.

But when I use

Bitmap bmp = BitmapFactory.decodeResource(getResources(), image);

It gives the following error: The method decodeResource(Resources, int) in the type BitmapFactory is not applicable for the arguments (Resources, Drawable)

How do I save the images I get from the internet into my gallery?

Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
MrBunni
  • 103
  • 1
  • 1
  • 8

0 Answers0