-1

I have a problem in API 8 with AsyncTask...

Here is my code...

try{
    new loadPhotos().execute(""+USER._id,"date","DESC");
}catch(Exception e){
    e.printStackTrace();
}

Before you ask... YES I am desperate, so I surrounded EVERYTHING with try{}catch

public class loadPhotos extends AsyncTask<String,Object,Void>{
            ProgressBar pb;

            ImageView img;
            TableRow.LayoutParams paramsImage;

            List<String> urlsOfImages;
            TableLayout tl;
            TableRow tr;
            LayoutParams lp;

            int numberOfPhotos=0;

            @Override
            protected void onPreExecute(){
                urlsOfImages = new ArrayList<String>(); //There is stored urls of all images ( www.example.com/image1.jpg ... etc)

                try {
                    allUserPhotoBitmaps.clear(); //allUserPhotoBitmaps is List<Bitmap>

                    pb = (ProgressBar) findViewById(R.id.profile_photos_progress_bar);
                    pb.setVisibility(View.VISIBLE);

                    tl = (TableLayout) findViewById(R.id.profile_photos_table_layout);
                    tl.removeAllViews();

                    tr = new TableRow(Profile.this);
                    lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

                    tr.setLayoutParams(lp);
                }catch(Exception e){
                    Toast.makeText(Profile.this, "Error in onPreExecute", Toast.LENGTH_LONG).show();
                }

            }

            @Override
            protected Void doInBackground(String... arg) {

                try{
                    // This function basicaly returns List<HashMap<String,String>> with informations about photos ( web_path, latitude, longitude, city, etc...)
                    allUserPhotoInformations = myDb.getUrlsOfUserImages(arg[0],arg[1],arg[2]); 
                }catch(Exception e){
                    Toast.makeText(Profile.this, "Error downloading images", Toast.LENGTH_SHORT).show();
                }

                if ( allUserPhotoInformations != null ) {
                    for (int i=0 ; i < allUserPhotoInformations.size() ; i++){
                        try{
                            Bitmap bm = getBitmapFromURL(allUserPhotoInformations.get(i).get("file_path"));
                            allUserPhotoBitmaps.add(bm);
                            publishProgress(bm,i);
                        }
                        catch(Exception e){
                            return null;
                        }
                    }
                }

                return null;
            }

            @Override
            protected void onProgressUpdate(Object... arg){

                img = new ImageView(getBaseContext());
                paramsImage = new TableRow.LayoutParams( (((int)screenWidth)/2)-16 , (((((int)screenWidth)/2)-15)/16)*11 );
                paramsImage.setMargins(10,10,0,0);

                img.setLayoutParams(paramsImage);
                img.setBackgroundColor(Color.parseColor("#FF000000"));

                try{
                    img.setImageBitmap((Bitmap) arg[0]);
                }catch(Exception e){
                    Toast.makeText(Profile.this,"Cannot put Bitmap into ImageView",Toast.LENGTH_LONG).show();
                }

                img.setVisibility(View.VISIBLE);

                img.setOnClickListener(new ImageListener((Integer)arg[1])); //here I passing index to onClickListener

                tr.addView(img);

                if ( numberOfPhotos==1 ){

                    try{
                        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                    }catch(Exception e){
                        e.printStackTrace();
                        Toast.makeText(Profile.this,"Cannot inflate tableLayout",Toast.LENGTH_LONG).show();
                    }

                    tr = new TableRow(Profile.this);
                    tr.setLayoutParams(lp);

                    numberOfPhotos--;
                }
                else {
                    numberOfPhotos++;
                }
            }
            @Override
            protected void onPostExecute(Void arg){
                pb.setVisibility(View.GONE);

                userPhotos.setText("uploaded: "+allUserPhotoBitmaps.size()+" photos");

                if ( numberOfPhotos==1 ){
                    try{
                        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                    }catch(Exception e){
                        e.printStackTrace();
                        Toast.makeText(Profile.this,"Cannot inflate tableLayout",Toast.LENGTH_LONG).show();
                    }

                    tr = new TableRow(Profile.this);
                    tr.setLayoutParams(lp);
                }
            }

In my device ( API > 15 ) everything works great, but in API 10 it crashes the app... I you want , I can provide also OnClickListener or some othere methods...

Yes, here ... How I download pictures :

public Bitmap getBitmapFromURL(String src) {
                try {
                    java.net.URL url = new java.net.URL(src);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoInput(true);
                    connection.connect();
                    InputStream input = connection.getInputStream();
                    Bitmap myBitmap = BitmapFactory.decodeStream(input);

                    double width;
                    double height;

                    width = (screenWidth)/myBitmap.getRowBytes();

                    if ( myBitmap.getWidth() > myBitmap.getHeight() ){
                        Log.w("size","landscape");
                        height = ((myBitmap.getRowBytes()*width)/16)*11;
                        width = (height/11)*16;
                    }
                    else {
                        Log.w("size","protrait");
                        height = ((myBitmap.getRowBytes()*width)/11)*16;
                        width = (height/16)*11;
                    }

                    Bitmap bitmap = Bitmap.createScaledBitmap(myBitmap, (int)width, (int)height, true);

                    return bitmap;
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(Profile.this, "Error parsing image to bitmap", Toast.LENGTH_LONG).show();
                    return null;
                }
            }

Any help ?

Michal Heneš
  • 353
  • 2
  • 4
  • 19

1 Answers1

1

call new loadPhotos().execute(""+USER._id,"date","DESC"); in onCreate and Use Asynctask and try to avoid load in main thread. Your application crash one of the main reason may be if you are calling the methode in main thread.
Ex:

String zoomouturl = "your url";

new ImageDownload().execute(zoomouturl);

Try

public class ImageDownload extends AsyncTask<String, Void, String> {

            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub
          bmOptions = new BitmapFactory.Options();
                bmOptions.inSampleSize = 1;
                loadBitmap(zoomouturl, bmOptions);
                return zoomouturl;
            }

            protected void onPostExecute(String imageUrl) {

                if (!imageUrl.equals("")) {

                     Constants._image.setImageBitmap(bm);


                } else {
                    Toast.makeText(this,
                            "not found proper bitmap.. ", Toast.LENGTH_LONG)
                            .show();
                }
            }

        }

and

        public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) {
            InputStream in = null;
            try {
                in = OpenHttpConnection(URL);
                bm = BitmapFactory.decodeStream(in, null, options);
                in.close();
            } catch (IOException e1) {
            }
            return bm;
        }

        private static InputStream OpenHttpConnection(String strURL)
                throws IOException {
            InputStream inputStream = null;
            URL url = new URL(strURL);
            URLConnection conn = url.openConnection();

            try {
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setRequestMethod("GET");
                httpConn.connect();

                if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    inputStream = httpConn.getInputStream();
                }
            } catch (Exception ex) {
            }
            return inputStream;
        }
Subhalaxmi
  • 5,687
  • 3
  • 26
  • 42