Android - 可绘制的图像到位图

时间:2013-06-18 23:16:20

标签: android bitmap drawable

我正在尝试将Drawable转换为bitmap,以便将其保存到我的Android设备上的外部SD卡中。

我使用以下内容来获取Drawable图像:

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

Drawable图片来自另一个类:

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

我在Stackoverflow上发现的方法是:

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

然而,这不起作用,因为我没有调用R.drawable.something而只是调用图像,这当然是Drawable

但是当我使用

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

它出现以下错误:The method decodeResource(Resources, int) in the type BitmapFactory is not applicable for the arguments (Resources, Drawable)

如何将从互联网上获取的图像保存到我的图库中?

0 个答案:

没有答案