How to write a file using Intent.ACTION_CREATE_DOCUMENT

前端 未结 1 1849
-上瘾入骨i
-上瘾入骨i 2020-12-19 18:35

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

相关标签:
1条回答
  • 2020-12-19 19:01

    You need to use 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 {
                    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();
                }
    
    }
    }
    
    0 讨论(0)
提交回复
热议问题