2013-11-13 6 views
0

Я работаю над созданием панели здоровья для игры и ее сбоев: Спасибо заранее!BitmapDrawable не может декодировать

//there a corresponding ImageView in the layout 
    healthLevel = (ImageView) findViewById(R.id.healthbar); 
    BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), "res/drawable/health.png"); 

    //vertical bar cropped from top 
    clipDrawable = new ClipDrawable(bitmapDrawable, Gravity.BOTTOM, ClipDrawable.VERTICAL); 
    healthLevel.setImageDrawable(clipDrawable); 

    HelperClass.setHealth(); 
    clipDrawable.setLevel(10000); 
    clipDrawable.invalidateSelf(); 

Вход:

11-12 19:32:46.272: W/BitmapDrawable(10524): BitmapDrawable cannot decode res/drawable/health.png 

Решение:

//there a corresponding ImageView in the layout 
    healthLevel = (ImageView) findViewById(R.id.healthbar); 
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.health); 

    //Convert Bitmap to Drawable 
    Drawable bitmapDrawable = new BitmapDrawable(getResources(),bmp); 

    //vertical bar cropped from top 
    clipDrawable = new ClipDrawable(bitmapDrawable, Gravity.BOTTOM, ClipDrawable.VERTICAL); 
    healthLevel.setImageDrawable(clipDrawable); 

    HelperClass.setHealth(); 
    clipDrawable.setLevel(10000); 
    clipDrawable.invalidateSelf(); 
+0

Вам нужно «BitmapDrawable»? Разве вы не можете использовать растровый или рисованный? – Emmanuel

+0

Я не знаю ... Я новичок в этом, поэтому я просто ухожу от других, которых я нашел – jcaruso

ответ

1

Что касается вашего вопроса, создание BitmapDrawable из файла пути необходим путь абсолютного хранения к файлу, а не относительный путь вашего пакета приложений.

Я пробовал следующие фрагменты кода, и он работает.

final ImageView iv = (ImageView) findViewById(R.id.image_view); 
    File file = new File("/storage/sdcard0/Download/ic_launcher.png"); 
    BitmapDrawable bw = new BitmapDrawable(file.getAbsolutePath()); 
    iv.setImageDrawable(bw); 

Однако, я думаю, что вам нужно что-то вроде Bitmap, который может быть создан с

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 

Вы можете найти this toturial и this link полезным.

Смежные вопросы