标签:
import android.Manifest; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.SystemClock; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.io.File; import java.io.IOException; import static android.content.ContentValues.TAG; public class MainActivity extends Activity { private Button btn3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn3 = (Button) findViewById(R.id.btn3); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean flag = PermissionsUtils.hasPermission( MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE); final String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }; if (flag == true) { writeFile(); } else { if (!shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { showMessageOKCancel("需要读取存储的权限", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { PermissionsUtils.requestPermission(MainActivity.this, PERMISSIONS_STORAGE); } } ); } else { PermissionsUtils.requestPermission(MainActivity.this, PERMISSIONS_STORAGE); } } } private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) { new AlertDialog.Builder(MainActivity.this) .setMessage(message) .setPositiveButton("OK", okListener) .setNegativeButton("Cancel", null) .create() .show(); } }); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case PermissionsUtils.REQUESTCODE: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission Granted writeFile(); } else { // Permission Denied Toast.makeText(this, "拒绝授权", Toast.LENGTH_SHORT).show(); } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } private void writeFile() { String fileName = "abc" + SystemClock.currentThreadTimeMillis() + ".txt"; File file = new File("/storage/emulated/0/" + fileName); try { file.createNewFile(); if (file.exists()) { Log.i(TAG, "writeFile: " + file.getAbsolutePath()); } } catch (IOException e) { e.printStackTrace(); } } private void readFile() { File file = new File("/storage/emulated/0/blank_500_500.png"); if (file.exists()) { Log.i(TAG, "readFile: " + file.getAbsolutePath()); } } }
import android.app.Activity; import android.content.pm.PackageManager; import android.support.v4.app.ActivityCompat; public class PermissionsUtils { final static int REQUESTCODE = 111; public static void requestPermission(Activity activity, String[] permisssions) { ActivityCompat.requestPermissions( activity, permisssions, REQUESTCODE ); } public static boolean hasPermission(Activity activity, String permission) { int permissionCode = ActivityCompat.checkSelfPermission( activity, permission); if (permissionCode != PackageManager.PERMISSION_GRANTED) { return false; } else { return true; } } }
标签:
原文地址:http://www.cnblogs.com/linson0116/p/5703223.html