android marshmallow问题从外部存储读取文件

时间:2016-08-31 10:00:55

标签: android storage

我尝试从我的外部存储器中的文件复制一个sqlite数据库这在Android版本(4和5)中运行良好,但在android 6(marshmallow)中不起作用为什么?请帮助,根据需要,在其清单中,请求以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

这是我的代码:

 if (Build.VERSION.SDK_INT >= 23) {
        if (ContextCompat.checkSelfPermission(Main2ActivityAdmin.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(Main2ActivityAdmin.this, android.Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(Main2ActivityAdmin.this,
                    new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE},
                    1);
        } else {
            Toast.makeText(Main2ActivityAdmin.this, "permission erreur", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(Main2ActivityAdmin.this, "ok", Toast.LENGTH_LONG).show();

    }
   private boolean copyDatabase(Context context) {


    try {
        //InputStream inputStream = context.getAssets().open(DatabaseHelper.DBNAME);

        File sdcard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File file = new File(sdcard, "sample.sqlite");
        InputStream  inputStream = null;
        inputStream = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));
        String outFileName = DatabaseHelper.DBLOCATION + DatabaseHelper.DBNAME;
        OutputStream outputStream = new FileOutputStream(outFileName);
        byte[]buff = new byte[1024];
        int length = 0;
        while ((length = inputStream.read(buff)) > 0) {
            outputStream.write(buff, 0, length);
        }
        outputStream.flush();
        outputStream.close();
        Log.w("MainActivity","DB copied");
        return true;
    }catch (Exception e) {
        e.printStackTrace();
        return false;
    } 

1 个答案:

答案 0 :(得分:0)

尝试此操作以放置权限

  

同时将WRITE_EXTERNAL_STORAGE更改为READ_EXTERNAL_STORAGE以获取读取权限

public class MyActivity extends AppCompatActivity {

        private static final int REQUEST_RUNTIME_PERMISSION = 123;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            if (CheckPermission(MyActivity .this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                // you have permission go ahead

            } else {
                // you do not have permission go request runtime permissions
                RequestPermission(MyDevIDS.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION);
            }
        }

        @Override
        public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {

            switch (permsRequestCode) {

                case REQUEST_RUNTIME_PERMISSION: {
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        // you have permission go ahead

                    } else {
                        // you do not have permission show toast.
                    }
                    return;
                }
            }
        }

        public void RequestPermission(MyActivity thisActivity, String Permission, int Code) {
            if (ContextCompat.checkSelfPermission(thisActivity,
                    Permission)
                    != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                        Permission)) {
                } else {
                    ActivityCompat.requestPermissions(thisActivity,
                            new String[]{Permission},
                            Code);
                }
            }
        }

        public boolean CheckPermission(MyActivity context, String Permission) {
            if (ContextCompat.checkSelfPermission(context,
                    Permission) == PackageManager.PERMISSION_GRANTED) {
                return true;
            } else {
                return false;
            }
        }
    }