获取权限
private void chenkAccess(){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
int access = ContextCompat.checkSelfPermission(getApplication(),Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(access== PackageManager.PERMISSION_GRANTED){
doSomeThing();
}else{
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},WRITE_EXTERNAL_STORAGE_GET);
}
}else{
doSomeThing();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode==WRITE_EXTERNAL_STORAGE_GET){
if(permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)
&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
doSomeThing();
}else{
Toast.makeText(this,"您不能继续使用此功能",Toast.LENGTH_LONG).show();
finish();
}
}
}
private void doSomeThing(){
Toast.makeText(this,"我真的使用这个权限作了很重要的工作",Toast.LENGTH_LONG).show();
}
json
public static void write_json(String json) {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file=Environment.getExternalStorageDirectory();
FileOutputStream out=null;
try {
out= new FileOutputStream(new File(file,"json.txt"));
out.write(json.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public static String read_json() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = Environment.getExternalStorageDirectory();
FileInputStream inputStream = null;
StringBuffer sb=new StringBuffer();
try {
inputStream=new FileInputStream(new File(file,"json.txt"));
byte[] b=new byte[1024];
int len=0;
while((len=inputStream.read(b))!=-1){
sb.append(new String(b,0,len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}else{
return "";
}
}
图片
public static Bitmap read_bitmap(String filename) {
Bitmap bitmap=null;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file=Environment.getExternalStorageDirectory();
File file1 = new File(file, filename);
bitmap= BitmapFactory.decodeFile(file1.getAbsolutePath());
}
return bitmap;
}
public static void write_bitmap(String url) {
new MyTask().execute(url);
}
static class MyTask extends AsyncTask<String,String,String> {
@Override
protected String doInBackground(String... strings) {
FileOutputStream out=null;
InputStream inputStream=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){
inputStream = connection.getInputStream();
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = Environment.getExternalStorageDirectory();
out = new FileOutputStream(new File(file,"xiaoyueyue.jpg"));
byte[] bytes=new byte[1024];
int len=0;
while((len=inputStream.read(bytes))!=-1){
out.write(bytes,0,len);
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(connection!=null){
connection.disconnect();
}
}
return null;
}
}
}