SD卡的四种操作
准备工作
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button writejson;
private Button readjson;
private Button urlpic;
private Button readpic;
private TextView showjson;
private ImageView showpic;
private String json = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
private String picurl="http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.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);
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<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="这里是显示读取的json字符串的"
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"/>
</LinearLayout>
写入json
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: 写入成功");
}
}
});
读取json
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());
}
}
});
从网上下载一张图片到SD卡中
urlpic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyTask().execute(picurl);
}
});
异步
package com.example.weekhomework1013;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
class MyTask extends AsyncTask<String,String,String> {
@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.