Android4.0 emulator to access the network on a picture, display and download, but if you do not remove
in Manifest.xml insideandroid: minSdkVersion = "8"
android: targetSdkVersion = "14" />
Will not be able to access the network, logcat message appears as follows:
If you remove the
Program code is as follows:
package com.example.urltest;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView iv;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
iv = (ImageView) findViewById (R.id.show);
/ / Define an image
try {
URL url = new URL ("http://i1.ce.cn/ce/xwzx/gnsz/gdxw/201303/01/W020130301275465307517.jpg");
/ / Open input stream
ResourcesInputStream is = url.openStream ();
/ / Parse out the pictures from the stream
Bitmap bitmap = BitmapFactory.decodeStream (is);
/ / Use ImageView display pictures
iv.setImageBitmap (bitmap);
is.close ();
/ / Open input stream
againis = url.openStream ();
/ / Open the phone files corresponding to the output stream
OutputStream os = openFileOutput ("student.jpg", MODE_WORLD_READABLE);
byte [] buff = new byte [1024];
int hasRead = 0;
while ((hasRead = is.read (buff))> 0) {
os.write (buff, 0, hasRead);
}
is.close ();
os.close ();
} Catch (Exception e) {
e.printStackTrace ();
}
}
@ Override
public boolean onCreateOptionsMenu (Menu menu) {
/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater () inflate (R.menu.main, menu);.
return true;
}
}
Reply:
log paste the text of it
Reply:
Ah, Log Picture not clear, paste the text better.
The targetSdkVersion little piecemeal tried yet?
Reply:
What is the problem, can not see ah. .
Reply:
minsdkversion piecemeal point version of the problem is not to try
Reply:
The network operating android4.0 android2.3 with different network operation, there is an updated, network operation can not run in the main program, you can learn more about the network operating under 4.0, http://blog.csdn.net/key123zhangxing / article/details/8187772, this is my reproduced, you can see
Reply:
Visit it with a child thread
Reply:
4.0 to access the network, it must be requested from another thread. Not in the main thread.
Is not this a relationship with
Reply:
Ha ha ha, can be removed to access it!
No comments:
Post a Comment