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

  • Home
  • SEARCH
  • 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 9007223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:37:48+00:00 2026-06-16T01:37:48+00:00

I have this issue with multiple Images upload with Android 3.x versions and above.

  • 0

I have this issue with multiple Images upload with Android 3.x versions and above. My app is hybrid, it’s not pure Android. I have to do Android coding using Phonegap interface. So here is the code which I have written using AsyncTask. It’s the same working fine for Android 2.3 but not working on Android 3.x and above. I am sharing my code.

public class CFileUploader extends Plugin {
    private String ACTION_POST_DATA = "cpost_data";
    private String CrLf = "\r\n";
    String base64Str=null;

    @Override
    public PluginResult execute(String arg0, JSONArray arg1, String arg2) {
        Log.e("Sample App", " IN EXECUTE METHOD ");

        PluginResult pluginResult = null;
        if (ACTION_POST_DATA.equals(arg0)) {
            try {
                upload(arg1.getString(0), arg1.getString(1), arg1.getString(2),
                        arg1.getString(3), arg1.getString(4));

            } catch (JSONException jsonex) {
                jsonex.printStackTrace();
            }
        }
        return pluginResult;
    }

    private PluginResult upload(String token, String date, String time,
            String email, String fileNames) {
        Log.e("Sample App", " IN UPLOAD METHOD ");
        PluginResult pluginres = null;

        final Data obje=new Data(token, date, time, email, fileNames);

        CFileUploader.this.ctx.runOnUiThread(new Runnable(){
            public void run(){
                MyImageTask mTask=new MyImageTask();
                mTask.execute(obje);
            }
        });

        return pluginres;
    }

    class Data {
        String token, date, time, email, fileNames;
        Data(String token, String date, String time, String email, String fileNames){
            this.token=token;
            this.date=date;
            this.time=time;
            this.email=email;
            this.fileNames=fileNames;
        }

    }

    private class MyImageTask extends AsyncTask<Data, String, String>{

        @Override
        protected String doInBackground(Data... params) {               
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;
            ByteArrayOutputStream baos = null;
            byte[] imgData = null;
            String urlString = "https://www.sampledata.com/myapp/upload_snapshots.php";

            Log.e("Sample App", " token " + params[0].token + " " + "date " + params[0].date + " "
                    + " time " + params[0].time + " " + "email " + params[0].email);
            Log.e("Sample App", " imgPath " + params[0].fileNames);

            try {
                URL url = new URL(urlString);
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("Content-Type",
                        "multipart/form-data;boundary=---------------------------1177141514664");

                String msg = "";
                StringBuffer buffer = new StringBuffer(msg);
                buffer.append("-----------------------------1177141514664");
                buffer.append(CrLf);
                buffer.append("Content-Disposition: form-data; name=\"token\";"
                        + CrLf);
                buffer.append(CrLf);
                buffer.append(params[0].token + CrLf);

                buffer.append("-----------------------------1177141514664");
                buffer.append(CrLf);
                buffer.append("Content-Disposition: form-data; name=\"date\";"
                        + CrLf);
                buffer.append(CrLf);
                buffer.append(params[0].date + CrLf);

                buffer.append("-----------------------------1177141514664");
                buffer.append(CrLf);
                buffer.append("Content-Disposition: form-data; name=\"time\";"
                        + CrLf);
                buffer.append(CrLf);
                buffer.append(params[0].time + CrLf);

                buffer.append("-----------------------------1177141514664");
                buffer.append(CrLf);
                buffer.append("Content-Disposition: form-data; name=\"MAX_FILE_SIZE\";"
                        + CrLf);
                buffer.append(CrLf);
                buffer.append("100000000072000" + CrLf);

                buffer.append("-----------------------------1177141514664");
                buffer.append(CrLf);
                buffer.append("Content-Disposition: form-data; name=\"email\";"
                        + CrLf);
                buffer.append(CrLf);
                buffer.append(params[0].email + CrLf);

                buffer.append("-----------------------------1177141514664");
                buffer.append(CrLf);
                buffer.append("Content-Disposition: form-data; name=\"appkey\";"
                        + CrLf);
                buffer.append(CrLf);
                buffer.append("426C3A7D5992B838BAF1BD10594C920C" + CrLf);

                buffer.append("-----------------------------1177141514664");
                buffer.append(CrLf);
                buffer.append("Content-Disposition: form-data; name=\"method\";"
                        + CrLf);
                buffer.append(CrLf);
                buffer.append("upload.snapshots" + CrLf);

                String msg1 = "";
                StringBuffer imgBuffer = new StringBuffer(msg1);
                List<byte[]> byetsInfo = new ArrayList<byte[]>();
                ArrayList<String> filenames = new ArrayList<String>();
                try {
                    JSONObject jObj = new JSONObject(new String(params[0].fileNames));
                    JSONArray jArray = jObj.getJSONArray("snapshot_images");
                    String drPath = android.os.Environment
                            .getExternalStorageDirectory().toString();

                    for (int i = 0; i < jArray.length(); i++) {
                        String img = jArray.getString(i);
                        Log.e("Sample app", " imageName " + img);

                        File f = new File(drPath + "/myapp_images/" + img);
                        Uri ur = Uri.fromFile(f);
                        filenames.add(img);
                        Bitmap bmp;
                        try {
                            bmp = Media.getBitmap(CFileUploader.this.cordova
                                    .getActivity().getContentResolver(), ur);
                            baos = new ByteArrayOutputStream();
                            bmp.compress(CompressFormat.JPEG, 90, baos);

                            imgData = baos.toByteArray();
                            Log.e("Sample app", " img data size " + imgData.length);

                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        byetsInfo.add(imgData);

                    }

                } catch (JSONException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                String msg3 = "";
                StringBuffer eofBuffer = new StringBuffer(msg3);
                eofBuffer.append(CrLf);
                eofBuffer.append("-----------------------------4664151417711--");
                eofBuffer.append(CrLf);

                conn.setChunkedStreamingMode(0);

                for (int i = 0; i < byetsInfo.size(); i++) {
                    dos = new DataOutputStream(conn.getOutputStream());

                    imgBuffer.delete(0, imgBuffer.length());
                    imgBuffer.append("-----------------------------4664151417711");
                    imgBuffer.append(CrLf);
                    imgBuffer.append("Content-Disposition: form-data; name=\"snapshotUpload[]\"; filename=\""
                            + filenames.get(i) + "\"" + CrLf);
                    imgBuffer.append("Content-Type: image/jpeg" + CrLf);
                    imgBuffer.append(CrLf);

                    dos.write(buffer.toString().getBytes());
                    dos.write(imgBuffer.toString().getBytes());

                    int index = 0;
                    int size = 1024;
                    do {

                        if ((index + size) < byetsInfo.get(i).length) {
                            size = byetsInfo.get(i).length - index;
                        }
                        dos.write(byetsInfo.get(i), index, size);
                        index += size;
                    } while (index < byetsInfo.get(i).length);
                    Log.e("file upload ", " written: " + index);

                    dos.write(eofBuffer.toString().getBytes());

                }

                Log.e("Debug", "File is written");
                Log.e("activity upload demo ",
                        " in file upload " + conn.getResponseMessage());
                dos.flush();

            } catch (Exception ec) {
                ec.printStackTrace();
            }

            // Read the response
            try {
                inStream = new DataInputStream(conn.getInputStream());
                char buff = 512;
                int len;
                byte[] data = new byte[buff];
                do {
                    len = inStream.read(data);
                    if (len > 0) {
                        System.out.println(new String(data, 0, len));
                        base64Str += new String(data, 0, len);
                        Log.e("Sample app", "  " + new String(data, 0, len));
                    }
                } while (len > 0);
                Log.e("file upload ", " DONE ");

                dos.close();
                inStream.close();

            } catch (Exception ex) {
                ex.printStackTrace();
            }

            try {
                if (conn.getResponseMessage().equalsIgnoreCase("OK")) {
                    return base64Str;
                } else {
                    return null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
        }

    }

}

The token, date, time, email and fileNames I will be getting from js file.

I should get a base64 string along with this :

Submitted Requests: 
requests={"imageInfo":{"snapshotDateTime":"2012-12-09 22:38:14","snapshots":{"name":["Water lilies.jpg","Sunset.jpg"],"type":["image\/jpeg","image\/jpeg"],"tmp_name":["\/tmp\/phpXmZchs","\/tmp\/phpqylUgX"],"error":[0,0],"size":[83794,71189]}}}

I am not getting either of them when I run this code in android 3.x and above.

Please correct me, if I am going wrong somewhere.Thanks a lot.

  • 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-16T01:37:49+00:00Added an answer on June 16, 2026 at 1:37 am

    whoever has answered thanks a lot. After struggling for some days, I finally found out the solution and its just a single line of code which is doing all the mess.

    conn.setChunkedStreamingMode(0);
    

    I have removed the above line and it’s working fine on all devices irrespective of versions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this issue: first, I execute a SQL query using Java and then
I have multiple images stored at URLs like: /uploads/hash/IMAGE001.jpg . Using jQuery UI's sortable()
This isn't a super critical issue, but in my project, I have multiple targets
I have a form where users can enter multiple images to upload along with
I have a script that generates multiple images from a external site using jQuery.
I have this issue trying to get all the text nodes in an HTML
I have this issue with ComboBox that uses IEnumerable<Brush> as ItemsSource; the problem lies
I have this issue, I'm have a user selected in LDAP through ldap search,
I just have this issue with reading from a network stream in C#. Since
I already have this issue but I cannot remember how to solved it. (I

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.