Android

안드로이드(Android) Bitmap을 Drawable 변환 방법(Drawable ↔ Bitmap)

정리를 하고자 남긴다.

Drawable drawableImg = getResources().getDrawable(R.drawable.intro_image);

// drawable 타입을 bitmap으로 변경
Bitmap bitmap = ((BitmapDrawable)drawableImg).getBitmap();

// bitmap 타입을 drawable로 변경
Drawable drawableImg2 = new BitmapDrawable(bitmap); 

미디어 저장소의 앨범이미지를 가져와 Bitmap으로 변환

    public static Bitmap getAlbumArt(Context _context, String _uriStr)
    {
        ContentResolver cr = _context.getContentResolver();
        InputStream in = null;
        try {
            in = cr.openInputStream(Uri.parse("content://media" + _uriStr));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (Exception ee){
            ee.printStackTrace();
            return null;
        }

        if( in != null) {
            Bitmap artwork = BitmapFactory.decodeStream(in);
            return artwork;
        }
        return null;
    }

[참고]
BitmapDrawable(영문)

https://developer.android.com/guide/topics/resources/drawable-resource?hl=ko

https://stackoverflow.com/questions/2415619/how-to-convert-a-bitmap-to-drawable-in-android

Leave a Reply

error: Content is protected !!