2017-07-19 134 views
0

我已经通过代码捕获屏幕截图并将其保存在下载目录中,但保存的文件不可见。无法在下载文件夹中看到保存的文件

当我重新启动手机,我得到它在下载目录中可见。

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

谢谢。为节省屏幕

法拍

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(); 
    } 

} 

回答

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) { 
       } 

这将刷新您的画廊。

+0

好,会尽量让你知道。谢谢:) –

+0

它的工作,谢谢。 –

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); 
相关问题