Aspicas
Aspicas

Reputation: 4497

Only the original thread that created a view hierarchy can touch its views - Android

I'm using MultipartEntityBuilder to send images to php server, I take pics and videos to device camera to a directory, and I need to save files on new ArrayList and send it to php server.

Always it's reports an error:

error

my code

private class MyAsyncTask extends AsyncTask<String, Integer, Double>
    {
        @Override
        protected Double doInBackground (String... params)
        {
            String datos = value.getText().toString();

            HttpClient httpClient = getNewHttpClient();

            HttpPost httppost = new HttpPost("https://myurl.com");

            try
            {

path = Environment.getExternalStorageDirectory().toString() + "/mydirectory";

                File fileArray = new File(path);
                ArrayList<File> files = new ArrayList<File>(Arrays.asList(fileArray.listFiles()));

                int itemCount = files.size();

                MultipartEntityBuilder builder = MultipartEntityBuilder.create();

                builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

                for (int i = 0; i < itemCount; i++)
                {
                    builder.addPart("images[]", new FileBody(new File(path + "/" + num_img)));
                    num_img++;
                }

                builder.addTextBody("response", "prueba");

                HttpEntity entity = builder.build();

                httppost.setEntity(entity);

    //...............................

}

UPDATE

protected void onPostExecute (Double result)
        {
            path = Environment.getExternalStorageDirectory().toString() + "/myDirectory";


            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    File fileArray = new File(path);
                    ArrayList<File> files = new ArrayList<File>(Arrays.asList(fileArray.listFiles()));

                    int itemCount = files.size();
                    tv_files.setText("valor:" + itemCount);

                    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

                    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

                    builder.addTextBody("response", "prueba");

                    for (int i = 0; i < itemCount; i++)
                    {
                        builder.addPart("images[]", new FileBody(new File(path + "/" + num_img)));
                        num_img++;
                    }

                    HttpEntity entity = builder.build();

                }
            });

Upvotes: 1

Views: 445

Answers (1)

Sami Eltamawy
Sami Eltamawy

Reputation: 9999

This error is trying to say to you never touch the UI views outside any thread does not run on the UIThread.

In your DoInBackground you are trying to get the value of the EditText, and this is considering a touch for the UI views within a Thread does runs in the background.

SOLUTION: Move the following line

String datos = value.getText().toString();

Outside doInBackground method to onPreExcute() or on the constructor of the AsyncTask. Just declare String datos as a global variable in the AsyncTask and use it in your doInBackground() as you want

    @Override
    protected void onPreExecute() {
       datos = value.getText().toString();
    }

Upvotes: 1

Related Questions