将位图保存到SD卡而不创建任何文件

时间:2018-05-15 12:05:26

标签: android android-sdcard android-bitmap

当按下按钮时,我想将位图作为jpg文件保存到我的SD卡。当我运行以下代码时,我没有收到任何错误,但是当我检查手机上的SD卡时,没有创建任何文件。我做错了什么?

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (permission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
            }

            String root = Environment.getExternalStorageDirectory().toString();
            //String root = System.getenv("SECONDARY_STORAGE");
            File myDir = new File(root + "/Screenshots");
            myDir.mkdirs();
            Random generator = new Random();
            int n = generator.nextInt(10000);
            String fname = n+".jpg";
            File file = new File(myDir, fname);
            Log.d("DD",""+file);
            if (file.exists()) {
                file.delete();
            }
            try {
                FileOutputStream out = new FileOutputStream(file);
                rBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();

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

我在清单中有权限,但仍然没有生成文件:

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

我做了一些研究,建议getExternalStorageDirectory()不提供SDcard的路径,但以下替代方案给出了null:

String root = System.getenv(“SECONDARY_STORAGE”);

1 个答案:

答案 0 :(得分:0)

找到@CommonsWare指出的调用MediaScannerConnection.scanFile的答案,因此MediaStore可以发现文件并将其列在文件资源管理器中。 @greenapps也建议文件在外部存储器0中,而不是在SD卡中。

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (permission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
                ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
            }

            String root = Environment.getExternalStoragePublicDirectory("").toString();
            //String root = System.getenv("SECONDARY_STORAGE");
            File myDir = new File(root + "/Screenshots");
            if (!myDir.exists())
                if (!myDir.mkdirs())
                    {Toast.makeText(activity, "make dir failed.", Toast.LENGTH_SHORT).show(); return;}
            Random generator = new Random();
            int n = generator.nextInt(10000);
            String fname = n+".jpg";
            File file = new File(myDir, fname);
            Log.d("DD",""+file);
            try {
                FileOutputStream out = new FileOutputStream(file);
                rBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            //add mediastore discovery
            MediaScannerConnection.scanFile(activity, new String[] {file.getAbsolutePath()},new String[] {null}, null);
        }
    }