Good,
I would like in my application to be able to change the image of an ImageView without having to update the app so often. I have thought of saving the image on a web server and downloading the image every time the app is opened. So when I change the image on the server, it will be changed in the app.
The problem is that when trying to change the image it gives me an error.
The code is the following:
public class FragmentInicio extends Fragment {
private static URL imgurl;
public FragmentInicio() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_inicio, container, false);
ImageView logo = (ImageView) v.findViewById(R.id.imageEccaInicio);
try {
imgurl = new URL("https://s-media-cache-ak0.pinimg.com/236x/22/c8/9e/22c89e308d734b5daa9f383419b1bdc5.jpg");
Bitmap mIcon_val = BitmapFactory.decodeStream(imgurl.openConnection().getInputStream());
logo.setImageBitmap(mIcon_val);
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
logo.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//Toast.makeText(view.getContext(), "Se ha tocado la imagen", Toast.LENGTH_LONG).show();
//Llama al método openDrawer de la actividad principal para abrir el menú lateral.
((MainActivity)getActivity()).openDrawer();
}
});
return v;
}
}
If I comment everything that is inside the try, the application takes the layout image by default and does not give an error. But I am interested in the image being taken from the url that I pass to it.
This is the error it gives:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: ecca.radio, PID: 2839
java.lang.RuntimeException: Unable to start activity ComponentInfo{ecca.radio/ecca.radio.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
at ecca.radio.navigationDrawer.FragmentInicio.onCreateView(FragmentInicio.java:43)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Thank you very much for your help, I'm learning a lot.
As the documentation says , if you are using a version equal to or greater than
Honeycomb
, this error occurs because you are trying to execute the network operation on the main thread.Therefore, what you could do is wrap your code in the
doInBackground
una methodAsyncTask
, which is executed as an asynchronous task.The error seems to be due to the internet connection try this
EDIT
As @Error404 comments, this task must be called from an asynchronous task, since you are trying to execute a request operation from the internet in the main thread, adding to this idea you could try something like this
and you call her
EDIT 2
The NetworkOnMainThreadException error :
indicates that operations are being performed on the main thread, which is incorrect.
To avoid this you can make use of runOnUiThread() , Asynctask or also Handler.post() .
example
runOnUiThread()
:Example to use in your code:
There is a similar question on SO:
android.os.NetworkOnMainThreadException error in using Android's HttpURLConnection