I am currently developing an app. The app can handle customers and orders. I recently wrote a little function that creates a dummy customer in case an order got no customer yet. Customer contains bitmap,name,url,email. When I use that simple function to create that customer and then navigate to the listview that holds all my customers (2 in total for example) then it laggs incredibly. I get like 2 FPS and everything takes like 5 secounds to load. When I create the very same customer in the app itself in a GUI that I created for that task, it runs smooth as it is supposed to run. I don't know where that could come from.
Here is the CPU trace where I click on "Customers" in the navigation drawer:
The code to create the customer via the app:
Bitmap icon = ((BitmapDrawable) customerImage.getDrawable()).getBitmap();
Customer customer = new Customer(icon, name.getText().toString(), url.getText().toString(), email.getText().toString());
And the code where I create the customer directly in the MainActivity in the onCreate Function:
public static Customer getEmptyCustomer(Activity act) {
emptyCustomer = new Customer(Utility.getIconEmpty(act),"-","-","-");
return emptyCustomer;
}
The code is super simple and straightforward. I don't know what other parts of the code might be important for that issue. Can't think of any.
Hope someone knows that behaviour and can help a little.
EDIT:
Customer class:
public Customer(Bitmap logo, String name, String URL, String email) {
this.logo = logo;
this.name = name;
this.URL = URL;
this.email = email;
// arrayListCustomers.add(this);
addCustomer(this);
}
public Customer() {
}
// return 0 = everything good
// return 1 = name already exists
public int addCustomer(Customer c) {
boolean alreadyExists = false;
for (Customer customer : arrayListCustomers) {
if (customer.name.equals(c.name)) {
alreadyExists = true;
}
}
if (!alreadyExists) {
arrayListCustomers.add(c);
FileManager.saveCustomerToSdCard(c);
return 0;
} else {
return 1;
}
}
And the FileManager functions:
public static void saveCustomerToSdCard(Customer c) {
String filename = c.name+".json";
File customerDir = new File(Environment.getExternalStorageDirectory(), "Formicorn/Customer/");
if (!customerDir.exists()) {
if (!customerDir.mkdirs()) {
Log.d("App", "Failed to create customer directory");
}
}
Gson gson = new Gson();
String json = gson.toJson(c);
File file = new File(customerDir, filename);
try {
FileWriter writer = new FileWriter(file);
writer.write(json);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
saveLogoToSdCard(c);
}
private static void saveLogoToSdCard(Customer c) {
String filename = "logo_"+c.name+".png";
File logosDir = new File(Environment.getExternalStorageDirectory(), "Formicorn/Customer/Logos");
if (!logosDir.exists()) {
if (!logosDir.mkdirs()) {
Log.d("App", "Failed to create logo directory");
} else {
Log.d("App", "Successfully created logo directory");
}
}
File logoFile = new File(logosDir, filename);
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(logoFile);
c.logo.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Although the image is only 30kB its pixelsize is 1000x1000. Decreasing it to 200x200 pixels solved the problem. I still doubt, that the emulator is that weak or android can't handle that pixelsize. If anyone got additional thoughts to that, please share them.