-1

i have an application that uploads and get an image from parse.com

and i have got android.os.NetworkOnMainThreadException can anyone help me with it and tell me how to solve it?

Thanks

    final ParseFile fileObject = (ParseFile) parseObject.get("ImageFile");

    URL url = new URL(fileObject.getUrl());

    Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    imageView.setImageBitmap(bmp);

    linearLayout.addView(imageView);
Amjad Khader
  • 35
  • 1
  • 7
  • Possible duplicate of [How to fix android.os.NetworkOnMainThreadException?](http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception) – Mike M. Apr 10 '16 at 00:02

2 Answers2

0

Use your favorite image-loading library, such as Picasso, to download the image in the background and populate the ImageView asynchronously.

Or, do your own background work (e.g., AsyncTask) to download the image on a background thread, then update the ImageView on the main application thread, plus deal with caching and so forth.

It is possible that your final ParseFile fileObject = (ParseFile) parseObject.get("ImageFile"); code also needs to be done in the background; I have not used Parse and so do not know the details of its API.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0
Upload and Download Image(like Time consuming process) you should do that in Background thread.You tried to get or upload images in Main thread so got android.os.NetworkOnMainThreadException.Use Async task to do that

class ImageDownload extends AsyncTask<Object, Void, Object>
    {

        Bitmap myBitmap=null;
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            //ShowDialog();
        }

        @Override
        protected Object doInBackground(Object... params)
        {
           final ParseFile fileObject = (ParseFile) parseObject.get("ImageFile");

    URL url = new URL(fileObject.getUrl());

    Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

            return null;
        }

        @Override
        protected void onPostExecute(Object result)
        {
            super.onPostExecute(result);


            HideDialog()

           //Update UI here
           linearLayout.addView(imageView);
           imageView.setImageBitmap(bmp);

        }

    }
Raghavendra B
  • 441
  • 3
  • 10