Bitmap vs R.drawable


Bitmap vs R.drawable



I'm making an app in which I allow the users to upload their own images. The app first used R.drawable but since I wanted the users to upload their own images I had to change everything into Bitmap.



Now I'm getting an error and as far as I can now it's because the CPU is overloaded. Is it better to use R.drawable instead of Bitmaps for a RecycleView?



This is the code:


public void addBasicDices(){
if (DicesList == null){
DicesList = new ArrayList<>();
}
if (DicesList.size() < 8){
//Clears the list to avoid having twice the amount of dices in case of being less than 8 (E.g when I update the minimum amount of basic dices, adding d100 for example)
DicesList.clear();

//Add coin to DicesList
Dice2R.add(R.drawable.coin_tail);
Dice2R.add(R.drawable.coin_head);
diceicon = BitmapFactory.decodeResource(this.getResources(), R.drawable.coin_head);
for (int i = 0; i<Dice2R.size(); i++){
diceface = BitmapFactory.decodeResource(this.getResources(), Dice2R.get(i));
Dice2.add(diceface);
}
DicesList.add(new DiceClass(diceicon, Dice2));
//Add dice4 to DicesList
Dice4R.add(R.drawable.dice4_1);
Dice4R.add(R.drawable.dice4_2);
Dice4R.add(R.drawable.dice4_3);
Dice4R.add(R.drawable.dice4_4);
diceicon = BitmapFactory.decodeResource(this.getResources(), R.drawable.dice4_4);
for (int i = 0; i<Dice4R.size(); i++){
diceface = BitmapFactory.decodeResource(this.getResources(), Dice4R.get(i));
Dice4.add(diceface);
}
DicesList.add(new DiceClass(diceicon, Dice4));
//Add dice6 to DicesList
Dice6R.add(R.drawable.dice6_1);
Dice6R.add(R.drawable.dice6_2);
Dice6R.add(R.drawable.dice6_3);
Dice6R.add(R.drawable.dice6_4);
Dice6R.add(R.drawable.dice6_5);
Dice6R.add(R.drawable.dice6_6);
diceicon = BitmapFactory.decodeResource(this.getResources(), R.drawable.dice6_6);
for (int i = 0; i<Dice6R.size(); i++){
diceface = BitmapFactory.decodeResource(this.getResources(), Dice6R.get(i));
Dice6.add(diceface);
}
DicesList.add(new DiceClass(diceicon, Dice6));
//Add dice8 to DicesList
Dice8R.add(R.drawable.dice8_1);
Dice8R.add(R.drawable.dice8_2);
Dice8R.add(R.drawable.dice8_3);
Dice8R.add(R.drawable.dice8_4);
Dice8R.add(R.drawable.dice8_5);
Dice8R.add(R.drawable.dice8_6);
Dice8R.add(R.drawable.dice8_7);
Dice8R.add(R.drawable.dice8_8);
diceicon = BitmapFactory.decodeResource(this.getResources(), R.drawable.dice8_8);
for (int i = 0; i<Dice8R.size(); i++){
diceface = BitmapFactory.decodeResource(this.getResources(), Dice8R.get(i));
Dice8.add(diceface);
}
DicesList.add(new DiceClass(diceicon, Dice8));
//Add dice10 to DicesList
Dice10R.add(R.drawable.dice10_1);
Dice10R.add(R.drawable.dice10_2);
Dice10R.add(R.drawable.dice10_3);
Dice10R.add(R.drawable.dice10_4);
Dice10R.add(R.drawable.dice10_5);
Dice10R.add(R.drawable.dice10_6);
Dice10R.add(R.drawable.dice10_7);
Dice10R.add(R.drawable.dice10_8);
Dice10R.add(R.drawable.dice10_9);
Dice10R.add(R.drawable.dice10_10);
diceicon = BitmapFactory.decodeResource(this.getResources(), R.drawable.dice10_10);
for (int i = 0; i<Dice10R.size(); i++){
diceface = BitmapFactory.decodeResource(this.getResources(), Dice10R.get(i));
Dice10.add(diceface);
}
DicesList.add(new DiceClass(diceicon, Dice10));
//Add dice12 to DicesList
Dice12R.add(R.drawable.dice12_1);
Dice12R.add(R.drawable.dice12_2);
Dice12R.add(R.drawable.dice12_3);
Dice12R.add(R.drawable.dice12_4);
Dice12R.add(R.drawable.dice12_5);
Dice12R.add(R.drawable.dice12_6);
Dice12R.add(R.drawable.dice12_7);
Dice12R.add(R.drawable.dice12_8);
Dice12R.add(R.drawable.dice12_9);
Dice12R.add(R.drawable.dice12_10);
Dice12R.add(R.drawable.dice12_11);
Dice12R.add(R.drawable.dice12_12);
diceicon = BitmapFactory.decodeResource(this.getResources(), R.drawable.dice12_12);
for (int i = 0; i<Dice12R.size(); i++){
diceface = BitmapFactory.decodeResource(this.getResources(), Dice12R.get(i));
Dice12.add(diceface);
}
DicesList.add(new DiceClass(diceicon, Dice12));
//Add dice20 to DicesList
Dice20R.add(R.drawable.dice20_1);
Dice20R.add(R.drawable.dice20_2);
Dice20R.add(R.drawable.dice20_3);
Dice20R.add(R.drawable.dice20_4);
Dice20R.add(R.drawable.dice20_5);
Dice20R.add(R.drawable.dice20_6);
Dice20R.add(R.drawable.dice20_7);
Dice20R.add(R.drawable.dice20_8);
Dice20R.add(R.drawable.dice20_9);
Dice20R.add(R.drawable.dice20_10);
Dice20R.add(R.drawable.dice20_11);
Dice20R.add(R.drawable.dice20_12);
Dice20R.add(R.drawable.dice20_13);
Dice20R.add(R.drawable.dice20_14);
Dice20R.add(R.drawable.dice20_15);
Dice20R.add(R.drawable.dice20_16);
Dice20R.add(R.drawable.dice20_17);
Dice20R.add(R.drawable.dice20_18);
Dice20R.add(R.drawable.dice20_19);
Dice20R.add(R.drawable.dice20_20);
diceicon = BitmapFactory.decodeResource(this.getResources(), R.drawable.dice20_20);
for (int i = 0; i<Dice20R.size(); i++){
diceface = BitmapFactory.decodeResource(this.getResources(), Dice20R.get(i));
Dice20.add(diceface);
}
DicesList.add(new DiceClass(diceicon, Dice20));
//Add dice100 to DicesList
Dice100R.add(R.drawable.dice100_1);
Dice100R.add(R.drawable.dice100_2);
Dice100R.add(R.drawable.dice100_3);
Dice100R.add(R.drawable.dice100_4);
Dice100R.add(R.drawable.dice100_5);
Dice100R.add(R.drawable.dice100_6);
Dice100R.add(R.drawable.dice100_7);
Dice100R.add(R.drawable.dice100_8);
Dice100R.add(R.drawable.dice100_9);
Dice100R.add(R.drawable.dice100_10);
Dice100R.add(R.drawable.dice100_11);
Dice100R.add(R.drawable.dice100_12);
Dice100R.add(R.drawable.dice100_13);
Dice100R.add(R.drawable.dice100_14);
Dice100R.add(R.drawable.dice100_15);
Dice100R.add(R.drawable.dice100_16);
Dice100R.add(R.drawable.dice100_17);
Dice100R.add(R.drawable.dice100_18);
Dice100R.add(R.drawable.dice100_19);
Dice100R.add(R.drawable.dice100_20);
Dice100R.add(R.drawable.dice100_21);
Dice100R.add(R.drawable.dice100_22);
Dice100R.add(R.drawable.dice100_23);
Dice100R.add(R.drawable.dice100_24);
Dice100R.add(R.drawable.dice100_25);
Dice100R.add(R.drawable.dice100_26);
Dice100R.add(R.drawable.dice100_27);
Dice100R.add(R.drawable.dice100_28);
Dice100R.add(R.drawable.dice100_29);
Dice100R.add(R.drawable.dice100_30);
Dice100R.add(R.drawable.dice100_31);
Dice100R.add(R.drawable.dice100_32);
Dice100R.add(R.drawable.dice100_33);
Dice100R.add(R.drawable.dice100_34);
Dice100R.add(R.drawable.dice100_35);
Dice100R.add(R.drawable.dice100_36);
Dice100R.add(R.drawable.dice100_37);
Dice100R.add(R.drawable.dice100_38);
Dice100R.add(R.drawable.dice100_39);
Dice100R.add(R.drawable.dice100_40);
Dice100R.add(R.drawable.dice100_41);
Dice100R.add(R.drawable.dice100_42);
Dice100R.add(R.drawable.dice100_43);
Dice100R.add(R.drawable.dice100_44);
Dice100R.add(R.drawable.dice100_45);
Dice100R.add(R.drawable.dice100_46);
Dice100R.add(R.drawable.dice100_47);
Dice100R.add(R.drawable.dice100_48);
Dice100R.add(R.drawable.dice100_49);
Dice100R.add(R.drawable.dice100_50);
Dice100R.add(R.drawable.dice100_51);
Dice100R.add(R.drawable.dice100_52);
Dice100R.add(R.drawable.dice100_53);
Dice100R.add(R.drawable.dice100_54);
Dice100R.add(R.drawable.dice100_55);
Dice100R.add(R.drawable.dice100_56);
Dice100R.add(R.drawable.dice100_57);
Dice100R.add(R.drawable.dice100_58);
Dice100R.add(R.drawable.dice100_59);
Dice100R.add(R.drawable.dice100_60);
Dice100R.add(R.drawable.dice100_61);
Dice100R.add(R.drawable.dice100_62);
Dice100R.add(R.drawable.dice100_63);
Dice100R.add(R.drawable.dice100_64);
Dice100R.add(R.drawable.dice100_65);
Dice100R.add(R.drawable.dice100_66);
Dice100R.add(R.drawable.dice100_67);
Dice100R.add(R.drawable.dice100_68);
Dice100R.add(R.drawable.dice100_69);
Dice100R.add(R.drawable.dice100_70);
Dice100R.add(R.drawable.dice100_71);
Dice100R.add(R.drawable.dice100_72);
Dice100R.add(R.drawable.dice100_73);
Dice100R.add(R.drawable.dice100_74);
Dice100R.add(R.drawable.dice100_75);
Dice100R.add(R.drawable.dice100_76);
Dice100R.add(R.drawable.dice100_77);
Dice100R.add(R.drawable.dice100_78);
Dice100R.add(R.drawable.dice100_79);
Dice100R.add(R.drawable.dice100_80);
Dice100R.add(R.drawable.dice100_81);
Dice100R.add(R.drawable.dice100_82);
Dice100R.add(R.drawable.dice100_83);
Dice100R.add(R.drawable.dice100_84);
Dice100R.add(R.drawable.dice100_85);
Dice100R.add(R.drawable.dice100_86);
Dice100R.add(R.drawable.dice100_87);
Dice100R.add(R.drawable.dice100_88);
Dice100R.add(R.drawable.dice100_89);
Dice100R.add(R.drawable.dice100_90);
Dice100R.add(R.drawable.dice100_91);
Dice100R.add(R.drawable.dice100_92);
Dice100R.add(R.drawable.dice100_93);
Dice100R.add(R.drawable.dice100_94);
Dice100R.add(R.drawable.dice100_95);
Dice100R.add(R.drawable.dice100_96);
Dice100R.add(R.drawable.dice100_97);
Dice100R.add(R.drawable.dice100_98);
Dice100R.add(R.drawable.dice100_99);
Dice100R.add(R.drawable.dice100_100);
diceicon = BitmapFactory.decodeResource(this.getResources(), R.drawable.dice100_100);
for (int i = 0; i<Dice100R.size(); i++){
diceface = BitmapFactory.decodeResource(this.getResources(), Dice100R.get(i));
Dice100.add(diceface);
}
DicesList.add(new DiceClass(diceicon, Dice100));
}
}



Error:


06-29 20:07:20.902 17432-17432/plusultra.customdice A/Bitmap: Error, cannot access an invalid/free'd bitmap here!

--------- beginning of crash
06-29 20:07:20.902 17432-17432/plusultra.customdice A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 17432 (ltra.customdice)
06-29 20:07:20.979 17478-17478/? A/DEBUG: Revision: '0'
ABI: 'arm64'
pid: 17432, tid: 17432, name: ltra.customdice >>> plusultra.customdice <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
06-29 20:07:22.879 13510-17490/? E/STBIntentService: send error message:data_app_native_crash;1530295642760(pkgName:plusultra.customdice;version:1.0;versionCode:1) to BBS for ANR/FC/TOMB_STONE...





post the error please
– Alexandru Sandu
Jun 29 at 18:05




1 Answer
1



R.drawable works better cause it loads from the alternative resources folder qualifier (l, m, h, x, xx, xxx) while Bitmaps taken from camera or uploaded from storage can be a lot bigger making you run at two problems:



OR you layout will move slowly cause of large bitmaps being drawn on small views OR you run on OutOfMemoryException, both of those problems can be handled by downsampling the bitmap as can be seen at: https://developer.android.com/topic/performance/graphics/load-bitmap





I'll try, thanks.
– TommyTomato
Jun 29 at 18:18





Ok, it seems my error shows when I load the gson from memory, this is the error: 06-29 20:32:17.165 2218-19187/? E/ActivityManager: Found activity ActivityRecord{a9cb2aa u0 plusultra.customdice/.MainActivity t-1 f} in proc activity list using null instead of expected ProcessRecord{b50160f 19116:plusultra.customdice/u0a198}
– TommyTomato
Jun 29 at 18:35





Your error doesn't looks to be about images, how you parse and how you getting the result? There's no StackTrace? Also, if the gson is large returning it trough a Bundle (or Intent extras) can result in errors, in this case you have to implement another solution for the data (usually by adding data to a database and retrieving on the other side)
– Marcos Vasconcelos
Jun 29 at 19:29






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift