android sdk 小于等于28版本动态权限

读写权限

  1. private static final int REQUEST_EXTERNAL_STORAGE = 1;
  2. private static String[] PERMISSIONS_STORAGE = {
  3. "android.permission.READ_EXTERNAL_STORAGE",
  4. "android.permission.WRITE_EXTERNAL_STORAGE" };
  5. //然后通过一个函数来申请
  6. public static void verifyStoragePermissions(Activity activity) {
  7. try {
  8. //检测是否有写的权限
  9. int permission = ActivityCompat.checkSelfPermission(activity,
  10. "android.permission.WRITE_EXTERNAL_STORAGE");
  11. if (permission != PackageManager.PERMISSION_GRANTED) {
  12. // 没有写的权限,去申请写的权限,会弹出对话框
  13. ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
  14. }
  15. //检测是否有写的权限
  16. int permission2 = ActivityCompat.checkSelfPermission(activity,
  17. "android.permission.READ_EXTERNAL_STORAGE");
  18. if (permission2 != PackageManager.PERMISSION_GRANTED) {
  19. // 没有写的权限,去申请写的权限,会弹出对话框
  20. ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
  21. }
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. }

调用

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
verifyStoragePermissions(this);
#……………….
}

完整代码

  1. package com.example.myapplication;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.pm.PackageManager;
  5. import android.os.Environment;
  6. import android.support.v4.app.ActivityCompat;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.widget.TextView;
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.io.OutputStream;
  16. import java.util.List;
  17. public class MainActivity extends AppCompatActivity {
  18. // Used to load the 'native-lib' library on application startup.
  19. static {
  20. System.loadLibrary("native-lib");
  21. System.out.println(System.getProperty("java.library.path"));
  22. }
  23. // public static String assetFilePath(Context context, String assetName) {
  24. // File file = new File(context.getFilesDir(), assetName);
  25. // System.out.println("test android :"+file.getName());
  26. // if (file.exists() && file.length() > 0) {
  27. // System.out.println("test is exists android:"+file.getAbsolutePath());
  28. //
  29. // return file.getAbsolutePath();
  30. // }
  31. // System.out.println("test is not exists android:"+file.getAbsolutePath());
  32. // return null;
  33. // }
  34. public static String assetFilePath(Context context, String assetName) throws IOException {
  35. File file = new File(context.getFilesDir(), assetName);
  36. System.out.println("test android :"+file.getName());
  37. if (file.exists() && file.length() > 0) {
  38. System.out.println("test is exists android:"+file.getAbsolutePath());
  39. return file.getAbsolutePath();
  40. }
  41. System.out.println("test is not exists android:"+file.getAbsolutePath());
  42. try (InputStream is = context.getAssets().open(assetName)) {
  43. try (OutputStream os = new FileOutputStream(file)) {
  44. byte[] buffer = new byte[4 * 1024];
  45. int read;
  46. while ((read = is.read(buffer)) != -1) {
  47. os.write(buffer, 0, read);
  48. }
  49. os.flush();
  50. }
  51. return file.getAbsolutePath();
  52. }
  53. }
  54. @Override
  55. protected void onCreate(Bundle savedInstanceState) {
  56. super.onCreate(savedInstanceState);
  57. setContentView(R.layout.activity_main);
  58. verifyStoragePermissions(this);
  59. // 首先判断设备是否挂载SDCard
  60. boolean isMounted = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
  61. // if (isMounted) {
  62. // try {
  63. // writeFileToSDCard();
  64. // } catch (IOException e) {
  65. // e.printStackTrace();
  66. // Log.d("SDCard write error ", "test no SDCard!");
  67. //
  68. // }
  69. // } else {
  70. // Log.d("SDCard错误", "test no SDCard!");
  71. // }
  72. // try {
  73. // String fileName=assetFilePath(this,"76.png");
  74. // String test= gray(fileName,10,1000);
  75. // System.out.println("test is println android:"+test);
  76. //
  77. // } catch (IOException e) {
  78. // Log.e("PytorchHelloWorld", "test :::Error reading assets", e);
  79. // finish();
  80. // }
  81. int colors[] = {20,0,255};
  82. Segment("/sdcard/data/test_cuda.mnn","/sdcard/data/56.png","/sdcard/data/result.png","/sdcard/data/mask.png",colors,10);
  83. TextView tv = findViewById(R.id.sample_text);
  84. tv.setText(stringFromJNI());
  85. System.out.println("test system.out.print !");
  86. }
  87. /**
  88. * A native method that is implemented by the 'native-lib' native library,
  89. * which is packaged with this application.
  90. */
  91. private static final int REQUEST_EXTERNAL_STORAGE = 1;
  92. private static String[] PERMISSIONS_STORAGE = {
  93. "android.permission.READ_EXTERNAL_STORAGE",
  94. "android.permission.WRITE_EXTERNAL_STORAGE" };
  95. //然后通过一个函数来申请
  96. public static void verifyStoragePermissions(Activity activity) {
  97. try {
  98. //检测是否有写的权限
  99. int permission = ActivityCompat.checkSelfPermission(activity,
  100. "android.permission.WRITE_EXTERNAL_STORAGE");
  101. if (permission != PackageManager.PERMISSION_GRANTED) {
  102. // 没有写的权限,去申请写的权限,会弹出对话框
  103. ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
  104. }
  105. //检测是否有写的权限
  106. int permission2 = ActivityCompat.checkSelfPermission(activity,
  107. "android.permission.READ_EXTERNAL_STORAGE");
  108. if (permission2 != PackageManager.PERMISSION_GRANTED) {
  109. // 没有写的权限,去申请写的权限,会弹出对话框
  110. ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
  111. }
  112. } catch (Exception e) {
  113. e.printStackTrace();
  114. }
  115. }
  116. // 写一个文件到SDCard
  117. private void writeFileToSDCard() throws IOException {
  118. // 比如可以将一个文件作为普通的文档存储,那么先获取系统默认的文档存放根目录
  119. File parent_path = Environment.getExternalStorageDirectory();
  120. // 可以建立一个子目录专门存放自己专属文件
  121. File dir = new File(parent_path.getAbsoluteFile(), "datatest");
  122. dir.mkdir();
  123. System.out.println(parent_path.getAbsoluteFile());
  124. File file = new File(dir.getAbsoluteFile(), "myfile.txt");
  125. System.out.println(dir.getAbsoluteFile());
  126. Log.d("dir test ", " test " +dir.getAbsoluteFile());
  127. Log.d("文件路径 test ", file.getAbsolutePath());
  128. // 创建这个文件,如果不存在
  129. file.createNewFile();
  130. FileOutputStream fos = new FileOutputStream(file);
  131. String data = "hello,world!";
  132. byte[] buffer = data.getBytes();
  133. // 开始写入数据到这个文件。
  134. fos.write(buffer, 0, buffer.length);
  135. fos.flush();
  136. fos.close();
  137. Log.d("文件写入 test ", " test 成功");
  138. }
  139. public native String stringFromJNI();
  140. public native int Segment(String bufModel,String bufInput, String bufOuput,String bufMask,int[] color,int colorNum);
  141. }