In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to write SD card permissions in android6.0". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to write SD card permissions in android6.0" can help you solve your doubts.
The code is as follows:
Package com.example.admin.sdapplication;import android.Manifest;import android.app.Dialog;import android.content.DialogInterface;import android.content.pm.PackageManager;import android.os.Build;import android.os.Bundle;import android.os.Environment;import android.support.annotation.NonNull;import android.support.v4.app.ActivityCompat;import android.support.v4.content.ContextCompat;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button Import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;public class SdkActivity extends AppCompatActivity implements View.OnClickListener {request code for private Button btn_write; private Button btn_read; final int REQUEST_WRITE=1;// permission @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_sdk); btn_write= (Button) findViewById (R.id.btn_write) Btn_read= (Button) findViewById (R.id.btn_read); btn_read.setOnClickListener (this); btn_write.setOnClickListener (this);} private void showAlert () {Dialog alertDialog = new AlertDialog.Builder (this). SetTitle (permission description). SetMessage ("We need this permission to provide you with storage services"). SetIcon (R.drawable.ic_launcher). SetPositiveButton ("OK", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {/ / TODO Auto-generated method stub / / 2, apply for permission: parameter 2: array of permissions; Parameter 3: request code ActivityCompat.requestPermissions (SdkActivity.this,new String [] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE);}}). SetNegativeButton (cancel, new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {/ / TODO Auto-generated method stub}}). Create (); alertDialog.show ();} @ Override public void onClick (View view) {switch (view.getId ()) {case R.id.btn_read: String s = readToSdCard (); Log.i ("readTosdCard", s); break Case R.id.btn_write: / / determine whether phones above 6.0do not need if (Build.VERSION.SDK_INT > = 23) {/ / determine whether you have this permission if (ContextCompat.checkSelfPermission (this, Manifest.permission.WRITE_EXTERNAL_STORAGE)! = PackageManager.PERMISSION_GRANTED) {/ / the first request permission is canceled. Generally, you don't have to write if (ActivityCompat.shouldShowRequestPermissionRationale (SdkActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {Log.i ("readTosdCard", "We need this permission to provide you with storage services") ShowAlert ();} else {/ / 2. Apply permissions: parameter 2: an array of permissions Parameter 3: request code ActivityCompat.requestPermissions (this,new String [] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE);}} else {writeToSdCard ();}} else {writeToSdCard ();} break }} / / write data public void writeToSdCard () {/ / 1, determine whether the SD card is available if (Environment.getExternalStorageState (). Equals (Environment.MEDIA_MOUNTED)) {/ / SD card available / / 2, get SD card path File sdFile=Environment.getExternalStorageDirectory (); File path=new File (sdFile, "a.txt"); / / the a.txt file parameters under the SD card are preceded by the directory and followed by the file try {FileOutputStream fileOutputStream=new FileOutputStream (path) FileOutputStream.write ("hello" .getBytes ());} catch (Exception e) {e.printStackTrace ();} / / read data public String readToSdCard () {StringBuilder stringBuilder = null; if (Environment.getExternalStorageState (). Equals (Environment.MEDIA_MOUNTED)) {File sdFile=Environment.getExternalStorageDirectory (); File path=new File (sdFile, "a.txt"); stringBuilder = new StringBuilder (); try {FileInputStream fileInputStream=new FileInputStream (path); BufferedReader bufferedReader=new BufferedReader (new InputStreamReader (fileInputStream)) String str; while ((str=bufferedReader.readLine ())! = null) {stringBuilder.append (str);}} catch (Exception e) {e.printStackTrace ();}} return stringBuilder.toString () } / / determine whether the authorized method authorization is successful and directly call the write method which is the array of listening callback / / parameter context authorization results @ Override public void onRequestPermissionsResult (int requestCode, @ NonNull String [] permissions, @ NonNull int [] grantResults) {super.onRequestPermissionsResult (requestCode, permissions, grantResults); if (requestCode==REQUEST_WRITE&&grantResults [0] = PackageManager.PERMISSION_GRANTED) {writeToSdCard ();}
In fact, it is to judge whether the 6.0 phone is used to apply for permission and then use the callback mechanism to call write processing directly after the permission is applied.
The comments are clear. Just look at the code (⊙ o ⊙)!
In addition, it is important to note that we also deal with the first time the user denies permission, and when the second request is needed, the user is prompted for the role of permission.
/ / the judgment that the first request permission is canceled can generally not be written as if (ActivityCompat.shouldShowRequestPermissionRationale (SdkActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {Log.i ("readTosdCard", "We need this permission to provide you with storage services"); showAlert ();} else {/ / 2, apply permission: parameter 2: array of permissions Parameter 3: request code ActivityCompat.requestPermissions (this,new String [] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE);} read here, this article "how to write SD card permissions in android6.0" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.