android sdk 小于等于28版本动态权限
读写权限
private static final int REQUEST_EXTERNAL_STORAGE = 1;private static String[] PERMISSIONS_STORAGE = {"android.permission.READ_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE" };//然后通过一个函数来申请public static void verifyStoragePermissions(Activity activity) {try {//检测是否有写的权限int permission = ActivityCompat.checkSelfPermission(activity,"android.permission.WRITE_EXTERNAL_STORAGE");if (permission != PackageManager.PERMISSION_GRANTED) {// 没有写的权限,去申请写的权限,会弹出对话框ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);}//检测是否有写的权限int permission2 = ActivityCompat.checkSelfPermission(activity,"android.permission.READ_EXTERNAL_STORAGE");if (permission2 != PackageManager.PERMISSION_GRANTED) {// 没有写的权限,去申请写的权限,会弹出对话框ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);}} catch (Exception e) {e.printStackTrace();}}
调用
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    verifyStoragePermissions(this);
   #……………….
}
完整代码
package com.example.myapplication;import android.app.Activity;import android.content.Context;import android.content.pm.PackageManager;import android.os.Environment;import android.support.v4.app.ActivityCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.widget.TextView;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.List;public class MainActivity extends AppCompatActivity {// Used to load the 'native-lib' library on application startup.static {System.loadLibrary("native-lib");System.out.println(System.getProperty("java.library.path"));}// public static String assetFilePath(Context context, String assetName) {// File file = new File(context.getFilesDir(), assetName);// System.out.println("test android :"+file.getName());// if (file.exists() && file.length() > 0) {// System.out.println("test is exists android:"+file.getAbsolutePath());//// return file.getAbsolutePath();// }// System.out.println("test is not exists android:"+file.getAbsolutePath());// return null;// }public static String assetFilePath(Context context, String assetName) throws IOException {File file = new File(context.getFilesDir(), assetName);System.out.println("test android :"+file.getName());if (file.exists() && file.length() > 0) {System.out.println("test is exists android:"+file.getAbsolutePath());return file.getAbsolutePath();}System.out.println("test is not exists android:"+file.getAbsolutePath());try (InputStream is = context.getAssets().open(assetName)) {try (OutputStream os = new FileOutputStream(file)) {byte[] buffer = new byte[4 * 1024];int read;while ((read = is.read(buffer)) != -1) {os.write(buffer, 0, read);}os.flush();}return file.getAbsolutePath();}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);verifyStoragePermissions(this);// 首先判断设备是否挂载SDCardboolean isMounted = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);// if (isMounted) {// try {// writeFileToSDCard();// } catch (IOException e) {// e.printStackTrace();// Log.d("SDCard write error ", "test no SDCard!");//// }// } else {// Log.d("SDCard错误", "test no SDCard!");// }// try {// String fileName=assetFilePath(this,"76.png");// String test= gray(fileName,10,1000);// System.out.println("test is println android:"+test);//// } catch (IOException e) {// Log.e("PytorchHelloWorld", "test :::Error reading assets", e);// finish();// }int colors[] = {20,0,255};Segment("/sdcard/data/test_cuda.mnn","/sdcard/data/56.png","/sdcard/data/result.png","/sdcard/data/mask.png",colors,10);TextView tv = findViewById(R.id.sample_text);tv.setText(stringFromJNI());System.out.println("test system.out.print !");}/*** A native method that is implemented by the 'native-lib' native library,* which is packaged with this application.*/private static final int REQUEST_EXTERNAL_STORAGE = 1;private static String[] PERMISSIONS_STORAGE = {"android.permission.READ_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE" };//然后通过一个函数来申请public static void verifyStoragePermissions(Activity activity) {try {//检测是否有写的权限int permission = ActivityCompat.checkSelfPermission(activity,"android.permission.WRITE_EXTERNAL_STORAGE");if (permission != PackageManager.PERMISSION_GRANTED) {// 没有写的权限,去申请写的权限,会弹出对话框ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);}//检测是否有写的权限int permission2 = ActivityCompat.checkSelfPermission(activity,"android.permission.READ_EXTERNAL_STORAGE");if (permission2 != PackageManager.PERMISSION_GRANTED) {// 没有写的权限,去申请写的权限,会弹出对话框ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);}} catch (Exception e) {e.printStackTrace();}}// 写一个文件到SDCardprivate void writeFileToSDCard() throws IOException {// 比如可以将一个文件作为普通的文档存储,那么先获取系统默认的文档存放根目录File parent_path = Environment.getExternalStorageDirectory();// 可以建立一个子目录专门存放自己专属文件File dir = new File(parent_path.getAbsoluteFile(), "datatest");dir.mkdir();System.out.println(parent_path.getAbsoluteFile());File file = new File(dir.getAbsoluteFile(), "myfile.txt");System.out.println(dir.getAbsoluteFile());Log.d("dir test ", " test " +dir.getAbsoluteFile());Log.d("文件路径 test ", file.getAbsolutePath());// 创建这个文件,如果不存在file.createNewFile();FileOutputStream fos = new FileOutputStream(file);String data = "hello,world!";byte[] buffer = data.getBytes();// 开始写入数据到这个文件。fos.write(buffer, 0, buffer.length);fos.flush();fos.close();Log.d("文件写入 test ", " test 成功");}public native String stringFromJNI();public native int Segment(String bufModel,String bufInput, String bufOuput,String bufMask,int[] color,int colorNum);}
