布局
<Button
android:id="@+id/writejson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入json串"/>
<Button
android:id="@+id/readjson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取json串"/>
<Button
android:id="@+id/readpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="从SD卡中读取一张图片"/>
<Button
android:id="@+id/urlpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="从网络下载一张图片"/>
<TextView
android:id="@+id/showjson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="显示数据"
android:textColor="#000"
android:layout_marginTop="20dp"/>
<ImageView
android:id="@+id/showpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_marginTop="10dp"/>
网络请求
@Override
protected String doInBackground(String... strings) {
FileOutputStream fos = null;
InputStream is = null;
HttpURLConnection connection = null;
try {
URL url = new URL(strings[0]);
connection= (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5*1000);
connection.setReadTimeout(5*1000);
if (connection.getResponseCode() == 200){
is = connection.getInputStream();
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File externalStorageDirectory = Environment.getExternalStorageDirectory();
fos = new FileOutputStream(new File(externalStorageDirectory,"wangbolup.jpg"));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes))!=-1){
fos.write(bytes,0,len);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (fos!=null){
try {
fos.close();
} catch (IOException e) {
}
}
if (is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection!=null){
connection.disconnect();
}
}
return null;
}
网络下载数据,图片,和SD卡读取数据,图片
private Button writejson;
private Button readjson;
private Button urlpic;
private Button readpic;
private TextView showjson;
private ImageView showpic;
private String json = "http://www.181010.com/mmw/uploads/allimg/130702/2-130F2201934514.jpg";
private String picurl="http://www.181010.com/mmw/uploads/allimg/130702/2-130F2201934514.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String externalStorageState = Environment.getExternalStorageState();
final File externalStorageDirectory = Environment.getExternalStorageDirectory();
File externalStoragePublicDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
writejson = (Button) findViewById(R.id.writejson);
readjson = (Button) findViewById(R.id.readjson);
urlpic = (Button) findViewById(R.id.urlpic);
readpic = (Button) findViewById(R.id.readpic);
showjson = (TextView) findViewById(R.id.showjson);
showpic = (ImageView) findViewById(R.id.showpic);
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
writejson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (externalStorageState.equals(Environment.MEDIA_MOUNTED)){
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(externalStorageDirectory,"json.txt"));
fos.write(json.getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Log.i("tag", "onClick: 写入成功");
}
}
});
readjson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (externalStorageState.equals(Environment.MEDIA_MOUNTED)){
FileInputStream is = null;
StringBuffer sb = new StringBuffer();
try {
is = new FileInputStream(new File(externalStorageDirectory,"json.txt"));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes))!=-1){
sb.append(new String(bytes,0,len));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Log.i("tag", "onClick: 读取成功");
showjson.setText(sb.toString());
}
}
});
urlpic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyTask().execute(picurl);
}
});
readpic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = null;
if (externalStorageState.equals(Environment.MEDIA_MOUNTED)){
File file = new File(externalStorageDirectory, "wangbolup.jpg");
bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
}
showpic.setImageBitmap(bitmap);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100 &&grantResults[0] == PackageManager.PERMISSION_GRANTED){
}else{
finish();
Toast.makeText(this, "未获授权", Toast.LENGTH_SHORT).show();
}
}