2016-10-04 153 views
-1

您好,我使用手機屏幕製作了應用程序,但此應用程序將所有照片存儲在本地文件的內部存儲中,並且每當我在手機中打開我的圖庫時,每張照片都不顯示。如何將圖片保存到圖庫

那麼如何將每張照片存儲在圖庫文件夾中?

在這裏,我把SS

private void takeAScreenshot() { 
    Date now = new Date(); 
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); 
    try { 
     String mPath = Environment.getExternalStorageDirectory().toString() 
       + "/" + now + ".jpg"; 
     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(); 
    } catch (Throwable e) { 
     e.printStackTrace(); 
    } 
} 

我檢查權限

我還發現,其中從aparat保存的文件:

/存儲/模擬/ 0/DCIM /相機/ img_name .JPG

我覺得這個,還有這個工作:

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, mPath) 

但圖像是保存在庫文件夾和內部存儲,幫助

+0

檢查http://stackoverflow.com/questions/2169649/get-pick-an-image-from-androids-built-in-gallery-app-programmatically和http://blog.kupriyanov.com /2010/02/solved-android-save-image-to-media.html – Stallion

回答

2

保存後使用MediaScanner。

private void takeAScreenshot() { 
     Date now = new Date(); 
     android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); 
     try { 
      String mPath = Environment.getExternalStorageDirectory().toString() 
        + "/" + now + ".jpg"; 
      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(); 
     } catch (Throwable e) { 
      e.printStackTrace(); 
     } 
     MediaScannerConnection.scanFile(ActivityName.this, 
       new String[]{imageFile.toString()}, null, 
       new MediaScannerConnection.OnScanCompletedListener() { 
        public void onScanCompleted(final String path, final Uri uri) { 
         ActivityName.this.runOnUiThread(new Runnable() { 
          public void run() { 
           Intent shareIntent = new Intent(); 
           shareIntent.setAction(Intent.ACTION_SEND); 
           shareIntent.putExtra(Intent.EXTRA_STREAM, 
             uri); 
           shareIntent.setType("image/jpeg"); 
           startActivity(Intent.createChooser(shareIntent, 
             "choose one")); 
          } 
         }); 
        } 
       }); 
    } 
+0

這是很好的升級我的應用程序,plase檢查我的editet答案我有一個照片保存在2位置 – Rodriquez

+0

一旦文件保存一個您創建的文件路徑。第二是因爲你正在使用內容解析器創建第二個副本將圖像放入圖像數據庫中。 –

+0

如果它幫助你可以打勾標記或投票或同時開心編碼:) –

0

嘗試添加此:

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription); 

填寫您的詳細信息yourBitmap,yourTitle和yourDescription,或者只是把它作爲「」。

+0

不工作,請看看editet的答案 – Rodriquez

相關問題