[英]set image button - uri, android
我的程序生成幾個顯示鍵,我想分配每個圖標。 資產持有圖標。 但是android不會加載圖標
button.setText(shop.getName());
Drawable icon;
int s = shop.getId();
String sk = Integer.toString(s);
String imageUri = "file:///android_asset/shop"+sk+".png";
Log.w("imageURI", imageUri);
Uri uri=Uri.parse(imageUri);
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
icon = Drawable.createFromStream(inputStream, uri.toString() );
} catch (FileNotFoundException e) {
icon = getResources().getDrawable(R.drawable.shopping1);
}
Bitmap bitmap = ((BitmapDrawable) icon).getBitmap();
Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 100, 100, true));
button.setCompoundDrawablesWithIntrinsicBounds( null, d, null, null );
日志:... file:///android_asset/shop1.png
嘗試這個:
button.setText(shop.getName());
Drawable icon;
int s = shop.getId();
String sk = Integer.toString(s);
String imageUri = "file:///android_asset/shop"+sk+".png";
Log.w("imageURI", imageUri);
Uri uri=Uri.parse(imageUri);
InputStream is;
try {
is = this.getContentResolver().openInputStream( uri );
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 10;
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);
Drawable icon = new BitmapDrawable(getResources(),preview_bitmap);
} catch (FileNotFoundException e) {
//set default image from the button
icon = getResources().getDrawable(R.drawable.shopping1);
}
button.setBackground(icon);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.