abhinav
abhinav

Reputation: 537

query regarding image upload in android

When a button is clicked in my android application, it will open the camera application. When a picture is taken in the camera application, the picture is uploaded to server.

PROBLEM: When the picture is taken and "SAVE" is clicked in the camera application, it is not returning to the application until the picture is uploaded to the server.

This is because I am waiting for the Thread that is uploading the image to complete.

How to solve this problem? I want it to return to the application from the camera application and want to show a ProgressBar.

For that my procedure is :

--> Using Intent to open camera application like this:

Intent CameraIntent =new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(CameraIntent,CAMERA_PIC_REQUEST);

--> In the method onActivityResult(int requestCode, int resultCode, Intent data), I wrote the following code. I am encoding the image in Base64 and calling a Thread which uploads the Base64 encoded image to the server.

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode==CAMERA_PIC_REQUEST)
    {
            ThumbNail=(Bitmap) data.getExtras().get("data");
            ba=new ByteArrayOutputStream();

            ThumbNail.compress(Bitmap.CompressFormat.JPEG,100,ba);
            barray=ba.toByteArray();

            str_img=Base64.encodeToString(barray,Base64.DEFAULT);           

            if(resultCode==Activity.RESULT_OK )
            {               
            upload_image = new ThreadUploadImage(str_img);

            upload_image.start();

            while(upload_image.isAlive())
            {
                     Thread.sleep(100);
            }           
        }
    }
}

ThreadUploadImage.java

public class ThreadUploadImage extends Thread {

    String str_img,result;
    String port=null,ip_addr=null;

    public ThreadUploadImage(String str_img)
    {
        this.str_img=str_img;
    }

    public void run()
    {
        try
        {
            HttpClient client =new DefaultHttpClient();

            ip_addr=StaticVariables.ip_addr;
            port=StaticVariables.port;

            if(ip_addr!=null && port!=null)
            {
                List<NameValuePair> l =new ArrayList<NameValuePair>();

                l.add(new BasicNameValuePair("img_str",str_img));
                post.setEntity(new UrlEncodedFormEntity(l));    
                HttpResponse response=client.execute(post);

                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                StringBuilder builder = new StringBuilder();
                for (String line = null; (line = reader.readLine()) != null;) {
                    builder.append(line).append("\n");
                }

                this.result = builder.toString();
                Log.e("result_is",result);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

PROBLEM: When the picture is Taken and "SAVE" is clicked in the camera application, it is not returning to the application until the picture is uploaded to the server.

This is because I am waiting for the Thread which is uploading the image.

How to solve this problem? I want it to return to the application from the camera application and I want to show a ProgressBar.

Upvotes: 1

Views: 265

Answers (1)

David Manpearl
David Manpearl

Reputation: 12656

Remove the waiting for the Thread to finish.

Remove this:

while(upload_image.isAlive())
{
    Thread.sleep(100);
}

If you need to block the user from doing anything and if you need to inform him when the upload is finished, then you should put up a ProgressDialog with an indeterminate progress indicator (i.e. spinning wait icon).

When the Thread is complete, then you need to inform you Activity via simple callback or broadcast and dismiss() the ProgressDialog.

Callback Example:

public static interface MyCallback {
    public void finished(final boolean success);
}

Your Activity 'implements' MyCallback:

public class MyActivity implements MyCallback {
    ...
    public void finished(final boolean success) {
        if(success) {
            // Upload was successful!
        } else {
            // Upload failed.
        }
    }
}

Your ThreadUploadImage class must own a MyCallback object, and it must get set as a parameter in the constructor. Then, when the Thread finishes, you should call mCallback.finished().

public class ThreadUploadImage extends Thread   {

    String str_img,result;
    String port=null,ip_addr=null;
    final MyCallback mCallback;

    public ThreadUploadImage(String str_img, final MyCallback callback)
    {
        this.str_img=str_img;
        mCallback = callback;
    }

    public void run()
    {
        try
        {
            HttpClient client =new DefaultHttpClient();

            ...

            Log.e("result_is",result);
            mCallback.finished(true);
        } catch(Exception e) {
            e.printStackTrace();
            mCallback.finished(false);
        }
    }
}

Upvotes: 1

Related Questions