无法再在API 23中保存文件

时间:2016-09-27 04:32:20

标签: android-6.0-marshmallow

我们有一个应用程序可以将图像保存在SD卡的目录中。如果目录尚不存在,则创建该目录。

以下是在API 23到来之前完成工作的代码:

    OutputStream output;
    File filepath = Environment.getExternalStorageDirectory();

    File dir = new File(filepath.getAbsolutePath()
            + "/fingerprint/");
    dir.mkdirs();

    Bitmap bitmap = m_bitmap;

    File file = new File(dir, userId.getText().toString() + ".jpg");

    try {

        output = new FileOutputStream(file);

        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
        output.flush();
        output.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

由于某些原因,不再创建目录,并且不再在Android 6.0中保存文件。我们介绍了这些方法,以便在Marshmallow中使用API​​更改来更新我们的代码。

private void requestPermission(final Context context){
    if(ActivityCompat.shouldShowRequestPermissionRationale((Activity)context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        new AlertDialog.Builder(context)
                .setMessage(context.getResources().getString(R.string.permission_storage))
                .setPositiveButton(R.string.allow, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions((Activity) context,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                REQUEST_WRITE_EXTERNAL_STORAGE);
                    }
                }).show();

    } else {
        ActivityCompat.requestPermissions((Activity)context,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_WRITE_EXTERNAL_STORAGE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_WRITE_EXTERNAL_STORAGE: {
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(CaptureFingerprintActivity.this,
                        getResources().getString(R.string.permission_storage_success),
                        Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(CaptureFingerprintActivity.this,
                        getResources().getString(R.string.permission_storage_failure),
                        Toast.LENGTH_SHORT).show();
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
            return;
        }
    }
}

创建目录和保存文件的代码也会更新。

    OutputStream output;

    File storageDir = null;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {

        if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(CaptureFingerprintActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            storageDir = new File(Environment.Environment.getExternalStorageDirectory()

,"指纹");                 if(!storageDir.exists()){                     storageDir.mkdir();                 } else {                     requestPermission(CaptureFingerprintActivity.this);                 }

        }
        Bitmap bitmap = m_bitmap;

        File file = new File(storageDir, userId.getText().toString() + ".jpg");

        try {

            output = new FileOutputStream(file);

            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
            output.flush();
            output.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

这似乎仍然没有创建目录。

0 个答案:

没有答案