![](/img/trans.png)
[英]How to load url image to bitmap, convert it to drawable and display it with imageview
[英]How to load an image from drawable and convert to a bitmap
我在drawable文件夾中有一個圖像,我想轉換為位圖,然后轉換為byte []以存儲到數據庫中,我可以將位圖轉換為字節數組,但是我無法從drawable獲取圖像由於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);
將位圖設置為圖像時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 (project-level)
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-alpha5'
}
build.gradle (app-level)
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()
您可以通過一種非常簡單的方式來執行此操作。 像這樣將Glide添加到您的項目中-
implementation 'com.github.bumptech.glide:glide:4.9.0'
然后使用全局Bitmap變量並按如下方式調用-
專用位圖imageBitmap;
Glide.with(this)
.asBitmap()
.load(R.drawable.image)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
//Save the bitmap to your global bitmap
imageBitmap = resource;
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
現在將imageBitmap傳遞到getBitmapAsByteArray()以轉換為byteArray,以便可以將其保存在SQLite數據庫中。
希望能幫助到你!
用這個
Bitmap photo = ((BitmapDrawable) getDrawable(R.drawable.R.drawable.image)).getBitmap();
希望工作正常;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.