如何使用Intent.ACTION_CREATE_DOCUMENT写入文件 [英] How to write a file using Intent.ACTION_CREATE_DOCUMENT

查看:1283
本文介绍了如何使用Intent.ACTION_CREATE_DOCUMENT写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单来说,我想将视图组转换为jpg图像文件.由于Environment.getExternalStorageDirectory已被弃用,因此我使用此意图Intent.ACTION_CREATE_DOCUMENT

In simple words, I want to convert a viewgroup to a jpg image file. As Environment.getExternalStorageDirectory is deprecated, I use this intent Intent.ACTION_CREATE_DOCUMENT

private void createFile(String mimeType, String fileName) {
        Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType(mimeType);
        intent.putExtra(Intent.EXTRA_TITLE, fileName);
        startActivityForResult(intent, WRITE_REQUEST_CODE);
    }

onActivityResult();中,我得到结果返回的Uri. 我的问题是我会使用getExternalStorage()

In the onActivityResult(); I get the Uri returned by the result. My problem is that with getExternalStorage() I'd use

Bitmap bitmap = Bitmap.createBitmap(
                    containerLayout.getWidth(),
                    containerLayout.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            containerLayout.draw(canvas);
            FileOutputStream fileOutupStream = null;


            try {
                fileOutupStream = new FileOutputStream(fileName);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutupStream);
                fileOutupStream.flush();
                fileOutupStream.close();
                Toast.makeText(this, "saved " + fileName, Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(this, "something went wrong" + e.getMessage(), Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }

现在我得到了结果返回的Uri,但是,我不知道如何将所需的位图写入此Uri

@Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            if (resultCode == RESULT_OK && requestCode == WRITE_REQUEST_CODE) {
                Uri resultUri = data.getData();
//need help

}
}

推荐答案

您需要使用getContentResolver().openOutputStream

        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            if (resultCode == RESULT_OK && requestCode == WRITE_REQUEST_CODE) {
                FileOutputStream fileOutupStream = getContentResolver().openOutputStream(data.getData());
            try {
                fileOutupStream = new FileOutputStream(fileName);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutupStream);
                fileOutupStream.flush();
                fileOutupStream.close();
                Toast.makeText(this, "saved " + fileName, Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(this, "something went wrong" + e.getMessage(), Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }

}
}

这篇关于如何使用Intent.ACTION_CREATE_DOCUMENT写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆