android6.0文件存储权限问题
在Manifest中也要加入权限,不加也会出问题
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
SDFile
package com.chuanxidemo.shaoxin.demo06; import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Environment; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * Created by shaoxin on 2017/2/23. */ public class SDFile extends AppCompatActivity { private Button sdread; private Button sdwrite; private FileOutputStream outputStream; private static int REQUEST_EXTERNAL_STRONGE = 1; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sdfile); sdread = (Button) findViewById(R.id.sdread); sdwrite = (Button) findViewById(R.id.sdwrite); sdread.setOnClickListener(new MyClick()); sdwrite.setOnClickListener(new MyClick()); } //申请权限的方法包装,与clock 中的if语句的内容作用一样 // public static void verifyStoragePermissions(Activity activity) { // // if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STRONGE); // }//REQUEST_EXTERNAL_STRONGE是自定义个的一个对应码,用来验证请求是否通过 // } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//根据请求是否通过的返回码进行判断,然后进一步运行程序 if (grantResults.length > 0 && requestCode == REQUEST_EXTERNAL_STRONGE && grantResults[0] == PackageManager.PERMISSION_GRANTED) { writerSD(); } } public class MyClick implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.sdwrite: //获取SD路径 //判断是否有SD卡 if (ContextCompat.checkSelfPermission(SDFile.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(SDFile.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STRONGE); }//REQUEST_EXTERNAL_STRONGE是自定义个的一个对应码,用来验证请求是否通过 else { writerSD(); } break; case R.id.sdread: break; } } } //封装了数据写入SD卡的方法 public void writerSD() { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { } else { Toast.makeText(SDFile.this, "SD卡不存在", Toast.LENGTH_SHORT).show(); } File file = Environment.getExternalStorageDirectory(); File newfile = new File(file, "1.txt"); try { outputStream = new FileOutputStream(file); outputStream.write("123".getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
sdfile.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/sdread" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读文件" /> <Button android:id="@+id/sdwrite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写文件" /> </LinearLayout>