无法在下载文件夹中看到已保存的文件

时间:2017-07-19 09:43:58

标签: android

我已按代码捕获屏幕截图并将其保存在下载目录中,但保存的文件不可见。

当我重新启动手机时,我会在下载目录中看到它。

请建议如何解决这个问题。

谢谢。

保存屏幕截图的方法

  private void takeScreenshot() {
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    try {


        //  String folderPath = Environment.getExternalStorageDirectory().toString() + "/" + getString(R.string.app_name);
        String folderPath = Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DOWNLOADS + "/";

        if (!new File(folderPath).exists()) {
            new File(folderPath).mkdir();
        }

        // image naming and path  to include sd card  appending name you choose for file
        String mPath = folderPath + "/" + getString(R.string.app_name) + now + ".jpg";


        // create bitmap screen capture
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        File imageFile = new File(mPath);

        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

        Toast.makeText(this, "Screen shot saved at " + folderPath, Toast.LENGTH_SHORT).show();
        //openScreenshot(imageFile);

    } catch (Throwable e) {
        // Several error may come out with file handling or OOM
        e.printStackTrace();
    }

}

2 个答案:

答案 0 :(得分:1)

下载文件后尝试此操作:

                // refresh gallery
                try {
                    MediaScannerConnection.scanFile(getActivity(), new String[]{savedImagePath}, null, new MediaScannerConnection.OnScanCompletedListener() {
                        @Override
                        public void onScanCompleted(String path, Uri uri) {
                 //   ApplicationUtil.showToast(getActivity(), "onScanCompleted!");
                       }
                   });
                } catch (Exception e) {
                }

这将刷新您的图库。

答案 1 :(得分:0)

 public static void addImageToGallery(final String filePath, final Context context) {

    ContentValues values = new ContentValues();

    values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    values.put(MediaStore.MediaColumns.DATA, filePath);

    context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}

**保存图片后调用此方法**

 addImageToGallery(pathName, context);