java - 如何从可绘制图像加载图像并将其转换为位图

标签 java android image bitmap drawable

我在可绘制文件夹中有一个图像,我想转换为位图,然后转换为 byte[] 以存储到数据库中,我可以将位图转换为字节数组,但我无法获取来自可绘制的图像,因为 Bitmap photo = BitmapFactory.decodeResource(getResources(), R.drawable.image); 返回 null。怎么做?

我现在有什么

Bitmap photo = BitmapFactory.decodeResource(getResources(), R.drawable.image); //this returns null
if(photo != null)
byte[] bytes = getBitmapAsByteArray(photo);

//this works
DatabaseHelper databaseHelper = new DatabaseHelper(this);
databaseHelper.add("DEFAULT",bytes);

更新: 当我使用 photo.setImageBitmap(bitmap); 将位图设置为图像时,它从数据库检索信息后它不会出现,似乎商店不起作用

我像这样将信息存储到数据库中

private static byte[] getBitmapAsByteArray(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
        return outputStream.toByteArray();
    }

public boolean addUserInformation(String username, byte[] picture){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("USERNAME", username);
        if(picture!=null) {
            values.put("PICTURE", picture);
        }
        id = db.insert(TABLE_NAME1, null, values);
        }
        db.close();
        if(id == -1)
            return false;
        else
            return true;
    }

并检索这样的信息

Cursor data = databaseHelper.getUserInformation();
        if(data.moveToFirst()) {
            mUsername = data.getString(1);
            bytes = data.getBlob(2);
            if(bytes!=null)
                profilePhoto = BitmapFactory.decodeByteArray(bytes, 0, data.getBlob(2).length);
        }

在数据库类中使用 getUserInformation()

public Cursor getUserInformation(){
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "SELECT * FROM " + TABLE_NAME1;
        Cursor c = db.rawQuery(query,null);
        return c;
    }

最佳答案

以下方法应该可以正常工作:

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableId);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        drawable = (DrawableCompat.wrap(drawable)).mutate();
    }
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
            drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

build.gradle(项目级)

dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0-alpha5'
    }

build.gradle(应用级)

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
        vectorDrawables.useSupportLibrary = true
    }
    ...
}
...

或者,您也可以在 Kotlin 中使用 android-ktx,如下所示,它适用于 Drawable 的任何子类:

build.gradle

dependencies {
    implementation 'androidx.core:core-ktx:1.0.0-alpha1'
}

然后使用Drawable#toBitmap(),如下所示:

val bitmap = AppCompatResources.getDrawable(requireContext(), drawableId).toBitmap() 

关于java - 如何从可绘制图像加载图像并将其转换为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56823865/

相关文章:

java - Jetty setInitParameter 没有初始化任何参数

java - 使用 App Fuse 直接从数据库模式生成 CRUD 操作

java - 为什么在更改 Android 中的区域时 DateTimeZone.getDefault() 不会更新

java - 用于 Java 云应用程序的 Linux/Windows?

java - addView equivalente 但将 Preference 添加到 PreferenceScreen?

android - btk_FaceFinder_putDCR 中存在 FaceDetector 错误

html - CSS居中图像

c# - 在 c# .net 中检查 httppostedfilebase 图像宽度/高度的有效方法

python - 使用 Python 检查 OpenCV 中的像素颜色

java - FutureTask 取消()