📜 ⬆️ ⬇️

Revise old projects for Android 4.0

Today, when I wrote a new training example for my website, training on cats, I unexpectedly ran into one problem out of the blue. In the program code, it was necessary to realize the download of the image from the Internet and save it to the SD card. At first glance, the task presented no difficulties, but the project suddenly began to produce an error.




')
Here we must make a small digression. I started learning programming for Android relatively recently and started with the version of Android 2.3, since I had a phone with the same version. And all the projects I tried on the emulator under the same version. A couple of months ago, an update to ICS (Android 4.0) was released for my Samsung Galaxy SII and I began to create new projects for the new version, as well as rewrite my training examples for the new platform. Periodically, in Eclipse, when loading old projects, there were lines of code with strikethrough methods. It was clear that this method was outdated and the documentation should be re-read.

In my last case, nothing was emphasized, and the nature of the error was not entirely clear. In one of my lessons, I used a similar example of downloading an image from the network to ImageView, which worked fine before in Android 2.3. For the sake of interest, I launched this example under the Android 4.0 emulator, being absolutely confident in the performance of the example. But it was not there, and this example did not start. Replacing in the example one line

txtUrl.setText(e.toString()); 


I received a description of the error - android.os.NetworkOnMainThreadException. Then it was easier. Having this information and called on Google for help, I found out that, starting with Android 3.0, the rules have changed a bit. Now you can not access network resources in the main stream of activity. So I had to redo the example in a new way.

View code
 package ru.alexanderklimov.imagefrominet; import java.io.InputStream; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; public class ImageFromInetActivity extends Activity { private ImageView imgView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imgView = (ImageView) findViewById(R.id.imgView); // -  String url = "http://developer.alexanderklimov.ru/android/images/android_cat.jpg"; // show The Image new DownloadImageTask(imgView) .execute(url); } private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { ImageView bmImage; public DownloadImageTask(ImageView bmImage) { this.bmImage = bmImage; } protected Bitmap doInBackground(String... urls) { String urldisplay = urls[0]; Bitmap mIcon11 = null; try { InputStream in = new java.net.URL(urldisplay).openStream(); mIcon11 = BitmapFactory.decodeStream(in); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } return mIcon11; } protected void onPostExecute(Bitmap result) { bmImage.setImageBitmap(result); } } } 




In the revised version, the example has earned. Perhaps the code still needs some work, but it is already clear in which direction to move.
Surely, companies and serious developers are aware of this problem. The solution to the problem is already described in the documentation and on StackOverflow.com. Professionals do not write long operations in the main thread and they will not notice the problem at all. In my case, it came as a surprise to me, since in work projects there was no need to use downloading images from the Internet, but in the training example such “wrong” code was caught. And it seems that for many amateur programmers this can be a headache. Imagine the situation that your application is downloading a picture or other file from the Internet. And the part of users who were updated to ICS, starts to complain that nothing works. Another part of users with the same phone model will claim that everything works fine. Guess about the source of the problem will be quite difficult.

Conclusion: Revise your old projects for health under Android 4.0. Just yesterday, the statistics of devices for different versions of Android and Android 4 was updated already ahead of version 2.1. I fully admit that by the end of the year, the four will also move version 2.2. Therefore, we need to think about this problem right now.

PS During the experiments in solving the problem, no animals were injured.

Source: https://habr.com/ru/post/145092/


All Articles