Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8251837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:22:09+00:00 2026-06-08T00:22:09+00:00

I was originally using a custom method to upload some photos, but I decided

  • 0

I was originally using a custom method to upload some photos, but I decided to use an AsyncTask to upload the photos, because I’d like to display a ProgessBar and didn’t understand how to use one in my original method during the upload. When I execute my AsyncTask an IllegalArgumentException is thrown, but I don’t understand why.

private void doFileUpload(){

        Log.d("imagepath", "imagepath: " + imagepath);
                    finalPath = new File(imagepath);
                    if (!finalPath.exists()){
                        try {
                              finalPath.createNewFile();
                              copyFile(new File(filePath), finalPath);
                        } catch (IOException e) {
                                 // TODO Auto-generated catch block
                               e.printStackTrace();
                        }
                    }

                    Bitmap bmpPic = BitmapFactory.decodeFile(imagepath);
                    int MAX_IMAGE_SIZE = 300000; // max final file size
                    if ((bmpPic.getWidth() >= 600) && (bmpPic.getHeight() >= 600)) {
                          BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
                          bmpOptions.inSampleSize = 1;
                          while ((bmpPic.getWidth() >= 600) && (bmpPic.getHeight() >= 600)) {
                              bmpOptions.inSampleSize++;
                              bmpPic = BitmapFactory.decodeFile(filePath, bmpOptions);
                          }
                            Log.d("bmpOptions.inSampleSize", "Resize: " + bmpOptions.inSampleSize);
                        }
                        int compressQuality = 104; // quality decreasing by 5 every loop. (start from 99)
                        int streamLength = MAX_IMAGE_SIZE;
                        while (streamLength >= MAX_IMAGE_SIZE) {
                           ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
                           compressQuality -= 5;
                           Log.d("compressQuality", "Quality: " + compressQuality);
                           bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
                           byte[] bmpPicByteArray = bmpStream.toByteArray();
                           streamLength = bmpPicByteArray.length;
                           Log.d("streamLength", "Size: " + streamLength);
                        }
                    try {
                        FileOutputStream bmpFile = new FileOutputStream(finalPath);
                        bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpFile);
                        bmpFile.flush();
                        bmpFile.close();
                    } catch (Exception e) {
                        Log.e("ERROR", "Error on saving file");
                    }

        File file1 = new File(imagepath);
        try
        {

             HttpClient client = new DefaultHttpClient();
             HttpPost post = new HttpPost(URL);

             FileBody bin1 = new FileBody(file1,imageType);
             //String abc=bin1.getMediaType();
             //System.out.println("abc :"+abc);
             System.out.println("imageType :"+imageType);
             MultipartEntity reqEntity = new MultipartEntity();

             if(use.equals("EditData")){
                 reqEntity.addPart("uploadedfile1", bin1);
                 reqEntity.addPart("uID", new StringBody(md5_id));
                 reqEntity.addPart("uPWD", new StringBody(md5_pass));
                 reqEntity.addPart("TYPE", new StringBody("NWPHOTO"));
                 reqEntity.addPart("REF", new StringBody(Ref));
                 reqEntity.addPart("CAT", new StringBody(cat0));
                 System.out.println("imageType :"+imageType+" "+md5_id+" "+md5_pass+" "+Ref+" "+cat0);
             }else if(use.equals("NewData")){
                 reqEntity.addPart("uploadedfile1", bin1);
                 reqEntity.addPart("uID", new StringBody(md5_id));
                 reqEntity.addPart("uPWD", new StringBody(md5_pass));
                 reqEntity.addPart("TYPE", new StringBody("INSERTNW"));
                 reqEntity.addPart("Title", new StringBody(newTitle));
                 reqEntity.addPart("News", new StringBody(newNews));
                 reqEntity.addPart("CAT", new StringBody(cat0));
                 reqEntity.addPart("use", new StringBody("NewData"));
                 System.out.println("imageType :"+imageType+" "+md5_id+" "+md5_pass+" "+cat0+" "+newNews+" "+newTitle);
             }

             post.setEntity(reqEntity);
             HttpResponse response = client.execute(post);
             resEntity = response.getEntity();
             final String response_str = EntityUtils.toString(resEntity,"UTF-8");
             if (resEntity != null) {

                 Log.i("RESPONSE PHOTO",response_str);
                 runOnUiThread(new Runnable(){
                        public void run() {

                             try {
                                System.out.println("n Response photo from server : " + response_str);
                                Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
                            } catch (Exception e) {
                                e.printStackTrace();
                                Log.e("log_tag", "error"+e.toString());
                            }
                        }
                   });
             }
        }
        catch (Exception ex){
             Log.e("Debug", "error: " + ex.getMessage(), ex);
        }

}

Stack trace

07-17 11:22:34.715: E/AndroidRuntime(18379): FATAL EXCEPTION: main
07-17 11:22:34.715: E/AndroidRuntime(18379): java.lang.IllegalArgumentException: View not attached to window manager
07-17 11:22:34.715: E/AndroidRuntime(18379):    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:380)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:225)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at android.view.Window$LocalWindowManager.removeView(Window.java:432)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at android.app.Dialog.dismissDialog(Dialog.java:278)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at android.app.Dialog.access$000(Dialog.java:71)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at android.app.Dialog$1.run(Dialog.java:111)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at android.app.Dialog.dismiss(Dialog.java:268)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at com.planbiz.net.News_Edit$UploadImage.onPostExecute(News_Edit.java:1019)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at com.planbiz.net.News_Edit$UploadImage.onPostExecute(News_Edit.java:1)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at android.os.AsyncTask.finish(AsyncTask.java:417)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at android.os.Looper.loop(Looper.java:130)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at android.app.ActivityThread.main(ActivityThread.java:3687)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at java.lang.reflect.Method.invoke(Method.java:507)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-17 11:22:34.715: E/AndroidRuntime(18379):    at dalvik.system.NativeStart.main(Native Method)

My AsyncTask

class UploadImage extends AsyncTask<String, String, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(News_Edit.this);
                pDialog.setMessage("Loading ...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }

            protected String doInBackground(String... args) {
            Log.d("imagepath", "imagepath: " + imagepath);
                        finalPath = new File(imagepath);
                        if (!finalPath.exists()){
                            try {
                                  finalPath.createNewFile();
                                  copyFile(new File(filePath), finalPath);
                            } catch (IOException e) {
                                     // TODO Auto-generated catch block
                                   e.printStackTrace();
                            }
                        }

                        Bitmap bmpPic = BitmapFactory.decodeFile(imagepath);
                        int MAX_IMAGE_SIZE = 300000; // max final file size
                        if ((bmpPic.getWidth() >= 600) && (bmpPic.getHeight() >= 600)) {
                              BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
                              bmpOptions.inSampleSize = 1;
                              while ((bmpPic.getWidth() >= 600) && (bmpPic.getHeight() >= 600)) {
                                  bmpOptions.inSampleSize++;
                                  bmpPic = BitmapFactory.decodeFile(filePath, bmpOptions);
                              }
                                Log.d("bmpOptions.inSampleSize", "Resize: " + bmpOptions.inSampleSize);
                            }
                            int compressQuality = 104; // quality decreasing by 5 every loop. (start from 99)
                            int streamLength = MAX_IMAGE_SIZE;
                            while (streamLength >= MAX_IMAGE_SIZE) {
                               ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
                               compressQuality -= 5;
                               Log.d("compressQuality", "Quality: " + compressQuality);
                               bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
                               byte[] bmpPicByteArray = bmpStream.toByteArray();
                               streamLength = bmpPicByteArray.length;
                               Log.d("streamLength", "Size: " + streamLength);
                            }
                        try {
                            FileOutputStream bmpFile = new FileOutputStream(finalPath);
                            bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpFile);
                            bmpFile.flush();
                            bmpFile.close();
                        } catch (Exception e) {
                            Log.e("ERROR", "Error on saving file");
                        }

                File file1 = new File(imagepath);
                try
                {

                     HttpClient client = new DefaultHttpClient();
                     HttpPost post = new HttpPost(URL);

                     FileBody bin1 = new FileBody(file1,imageType);
                     //String abc=bin1.getMediaType();
                     //System.out.println("abc :"+abc);
                     System.out.println("imageType :"+imageType);
                     MultipartEntity reqEntity = new MultipartEntity();

                     if(use.equals("EditData")){
                         reqEntity.addPart("uploadedfile1", bin1);
                         reqEntity.addPart("uID", new StringBody(md5_id));
                         reqEntity.addPart("uPWD", new StringBody(md5_pass));
                         reqEntity.addPart("TYPE", new StringBody("NWPHOTO"));
                         reqEntity.addPart("REF", new StringBody(Ref));
                         reqEntity.addPart("CAT", new StringBody(cat0));
                         System.out.println("imageType :"+imageType+" "+md5_id+" "+md5_pass+" "+Ref+" "+cat0);
                     }else if(use.equals("NewData")){
                         reqEntity.addPart("uploadedfile1", bin1);
                         reqEntity.addPart("uID", new StringBody(md5_id));
                         reqEntity.addPart("uPWD", new StringBody(md5_pass));
                         reqEntity.addPart("TYPE", new StringBody("INSERTNW"));
                         reqEntity.addPart("Title", new StringBody(newTitle));
                         reqEntity.addPart("News", new StringBody(newNews));
                         reqEntity.addPart("CAT", new StringBody(cat0));
                         reqEntity.addPart("use", new StringBody("NewData"));
                         System.out.println("imageType :"+imageType+" "+md5_id+" "+md5_pass+" "+cat0+" "+newNews+" "+newTitle);
                     }

                     post.setEntity(reqEntity);
                     HttpResponse response = client.execute(post);
                     resEntity = response.getEntity();
                     response_str = EntityUtils.toString(resEntity,"UTF-8");
                }catch (Exception ex){
                     Log.e("Debug", "error: " + ex.getMessage(), ex);
                }
                return null;
            }

            protected void onPostExecute(String file_url) {
                if (resEntity != null) {
                    pDialog.dismiss();
                         Log.i("RESPONSE PHOTO",response_str);
                         runOnUiThread(new Runnable(){
                                public void run() {
                                    try {
                                        System.out.println("n Response photo from server : " + response_str);
                                        Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        Log.e("log_tag", "error"+e.toString());
                                    }
                                }
                           });
                     }
                }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:22:10+00:00Added an answer on June 8, 2026 at 12:22 am

    you dont need to start new threads in AsyncTask. Try doing all the tasks without using new threads. (like in your onPostExecute()

    AsyncTask enables proper and easy use of the UI thread. This class
    allows to perform background operations and publish results on the UI
    thread without having to manipulate threads and/or handlers.

    An asynchronous task is defined by a computation that runs on a
    background thread and whose result is published on the UI thread

    read the documentation for better understanding.

    also try replacing the context value in your toast

    Toast.makeText(News_Edit.this,"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I originally setup some conditions using CGRectIntersectsRect for some collision detection which worked fine.
I have several TextBoxes, which display angular values. I format these using different custom
I would like to convert a method to use generics. It is currently hard-coded
i'm trying to run Redmine using rubyEE, but I am constantly facing some action_controller
I was originally using SQLCE first starting with LINQ to SQL and then moved
In Fedora15, I was originally using vim , with all my settings defined in
I'm using MinGW (originally installed with mingw-get-inst-20120426.exe) in combination with Eclipse for C/C++ (Indigo
First time using Asp.net-mvc and originally followed the NerdDinner tutorial. My form submit button
I'm maintaining an MFC/COM/ATL 45-odd project solution originally written with VS6. I'm now using
I'm using EF 4.2 and originally I had rolled my own repository classes for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.