I am supposed to fetch images from internet and display it on my activity.For this i am using asynctask class.I forgot to mention INTERNET permission in my manifest file and the application is running succesfully without giving any exception and is able to fetch all the images on Emulator. On device it is showing Network error. My question is why emulator is behaving like this.I am using genymotion emulator (Google Nexus 4)
Code is like:-
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
Button btnClick = null;
Button btnClick2 = null;
Context context = MainActivity.this;
ImageView imageview = null;
ProgressBar progressBar;
String URL = "http://images.visitcanberra.com.au/images/canberra_hero_image.jpg";
String URLTiger = "http://www.hdwallpapersimages.com/wp-content/uploads/2014/01/Winter-Tiger-Wild-Cat-Images.jpg";
String URLTree = "http://blog.jimdo.com/wp-content/uploads/2014/01/tree-247122.jpg";
String URLSky = "http://www.ucl.ac.uk/news/news-articles/1213/muscle-fibres-heart.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnClick = (Button) findViewById(R.id.btnClick);
btnClick2 = (Button)findViewById(R.id.btnClick2);
imageview = (ImageView)findViewById(R.id.imageview);
progressBar = (ProgressBar)findViewById(R.id.progressbar);
progressBar.setVisibility(ProgressBar.GONE);
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnClick:
new ImageDownloader(this).execute("http://stylonica.com /wp-content/uploads/2014/02/nature.jpg");
new ImageDownloader(this).execute(URLTiger);
new ImageDownloader(this).execute(URLTree);
new ImageDownloader(this).execute(URLSky);
break;
case R.id.btnClick2:
new Thread(new Runnable() {
@Override
public void run() {
btnClick2.post(new Runnable() {
@Override
public void run() {
btnClick2.setText("Thread B");
}
});
}
}).start();
break;
}
}
public class ImageDownloader extends AsyncTask<String,String,Bitmap> {
ProgressDialog progressDialog;
Context context;
Bitmap bitmap;
public ImageDownloader(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
// progressDialog = ProgressDialog.show(context,"Loading Image ","Please wait....");
progressBar.setVisibility(ProgressBar.VISIBLE);
}
@Override
protected Bitmap doInBackground(String... args) {
try {
Log.d("new URL(args[0]..............", new URL(args[0]) + "");
bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap image) {
if(image != null){
imageview.setImageBitmap(image);
// progressDialog.dismiss();
progressBar.setVisibility(ProgressBar.INVISIBLE);
}else{
// progressDialog.dismiss();
progressBar.setVisibility(ProgressBar.INVISIBLE);
Toast.makeText(context, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
}
And I have not mentioned INTERNET permission in my manifest file.
Disclaimer: I work on Genymotion.
The answer is that it is a Genymotion bug. :(
Android API compliance is absolutely fundamental for Genymotion.
Please, accept our apologies for the time you lost on this.
We investigated this bug immediately and fixed it.
You will have the fix in our next release.
The problem only happen on version 2.3.7, 4.1.1 et 4.2.2.