Hi,
I've developed the following code, but now i get messages stating that the application main thread is getting too much load. I'm new to Android programming, but i've read that i should be doing the heavy load on a thread other that the main one. Can someone help me figuring out how to get all the image processing from the main thread to a worker thread (at least the decodeFile and the getResizedBitmap functions should be running there, i suppose...):
}
}
I've developed the following code, but now i get messages stating that the application main thread is getting too much load. I'm new to Android programming, but i've read that i should be doing the heavy load on a thread other that the main one. Can someone help me figuring out how to get all the image processing from the main thread to a worker thread (at least the decodeFile and the getResizedBitmap functions should be running there, i suppose...):
Code:
public class InputItemDataActivity extends AppCompatActivity {
static final int CAM_REQUEST = 1;
String baseFolderName = "ConservationApp";
String itemId = "";
String imageFileName = "";
ArrayList<String> allImages = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_item_data);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
itemId = intent.getStringExtra(AddItemActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.itemId);
textView.setText(itemId);
LoadImages(itemId);
}
public class ImageAdapter extends BaseAdapter{
private Context mContext;
public ImageAdapter(Context c){
mContext = c;
}
public int getCount(){
return allImages.size();
}
public Object getItem(int position){
return null;
}
public long getItemId(int position){
return 0;
}
public View getView(int position, View convertView, ViewGroup parent){
ImageView imageView;
if (convertView == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85,85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
}else {
imageView = (ImageView) convertView;
}
Bitmap bitmap = decodeFile(allImages.get(position));
imageView.setImageBitmap(bitmap);
Bitmap bitmapThumb = getResizedBitmap(bitmap, 85, 85);
imageView.setImageBitmap(bitmapThumb);
return imageView;
}
}
public static Bitmap decodeFile(String pathName) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
try {
bitmap = BitmapFactory.decodeFile(pathName, options);
Log.d("No Error", "Decoded successfully for sampleSize " + options.inSampleSize);
break;
} catch (OutOfMemoryError outOfMemoryError) {
// If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
Log.e("Error", "outOfMemoryError while reading file for sampleSize " + options.inSampleSize
+ " retrying with higher value");
}
}
return bitmap;
}
private static Bitmap getResizedBitmap(Bitmap bitmap, float maxWidth, float maxHeight) {
float width = bitmap.getWidth();
float height = bitmap.getHeight();
if (width > maxWidth) {
height = (maxWidth / width) * height;
width = maxWidth;
}
if (height > maxHeight) {
width = (maxHeight / height) * width;
height = maxHeight;
}
return Bitmap.createScaledBitmap(bitmap, (int) width, (int) height, true);
}
public void cancelAction(View view){
Intent intent = new Intent(this, AddItemActivity.class);
startActivity(intent);
}
public void captureImage(View view){
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_intent, CAM_REQUEST);
}
private File getFile(){
File folder = new File("sdcard/" + baseFolderName);
if(!folder.exists()){
folder.mkdir();
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
imageFileName = itemId + "_" + timeStamp + ".jpg";
File image_file = new File(folder, imageFileName);
return image_file;
}
private void InsertImageIntoDB(String itemId, String imageFilename){
SQLiteDatabase myDB = null;
String ItemTableName = "Image";
String erro = "";
String Data="";
// Cria a tabela principal
try {
// Declara a DB a usar
myDB = this.openOrCreateDatabase("ConservationAppDB", MODE_PRIVATE, null);
myDB.execSQL("INSERT INTO " + ItemTableName + " (QRCodeId, Image) VALUES ('" + itemId + "', '" + imageFilename + "');");
} catch(Exception e) {
Log.e("Error", "Error", e);
erro = e.getMessage();
TextView errorPanel = (TextView) findViewById(R.id.errorPanel);
errorPanel.setText("Insert Image Error: " + erro);
errorPanel.setVisibility(View.VISIBLE);
} finally {
if (myDB != null) {
myDB.close();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
String path="sdcard/" + baseFolderName + "/" + imageFileName;
// Add photo to DB
InsertImageIntoDB(itemId, imageFileName);
LoadImages(itemId);
}
private void LoadImages(String itemId){
SQLiteDatabase myDB= null;
/*
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
*/
String erro = "";
try {
// Declara a DB a usar
myDB = this.openOrCreateDatabase("ConservationAppDB", MODE_PRIVATE, null);
Cursor c = myDB.rawQuery("SELECT Image.ItemId, Image.QRCodeId, Image.Image FROM Image WHERE Image.QRCodeId = '" + itemId + "' AND Image.Image <> '';" , null);
int itemIdInt = c.getColumnIndex("ItemId");
int imageInt = c.getColumnIndex("Image");
// Check if our result was valid.
c.moveToFirst();
if (c != null) {
// Loop through all Results
do {
String currentImageFile = c.getString(imageInt);
String path="sdcard/" + baseFolderName + "/" + currentImageFile;
if(currentImageFile != ""){
// Prevent duplicates in adapter
if(allImages.contains(path)) {
Log.d("Image exists", path);
}else{
allImages.add(path);
}
//allImages.add(path);
Log.d("Path", path);
}else{
Log.d("Empty Path", path);
}
}while(c.moveToNext());
}else{
erro = "Não foram encontrados items na DB";
}
} catch(Exception e) {
Log.e("Error", "Error", e);
erro = e.getMessage();
} finally {
if (myDB != null) {
myDB.close();
}
}
if(erro != "") {
// Mostra texto de detalhe do erro
TextView errorPanel = (TextView) findViewById(R.id.errorPanel);
errorPanel.setText("Read Error: " + erro);
errorPanel.setVisibility(View.VISIBLE);
}else{
GridView gridView = (GridView) findViewById(R.id.ImageGrid);
try{
gridView.setAdapter(new ImageAdapter(this));
}catch (Exception e) {
Log.e("Error", "Error", e);
erro = e.getMessage();
TextView errorPanel2 = (TextView) findViewById(R.id.errorPanel);
errorPanel2.setText("GridView Error: " + erro);
}
}
}
}