2017-07-19 133 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); 
相關問題