2016-07-06 11 views
2

私はスクリーンショットを撮って、アプリケーション名でストレージに保存しています。何もエラーを出さずにストレージに写真を保存していますが、携帯電話を再起動するとギャラリーに表示されます。ギャラリーに画像を保存する

は写真のための方法を保存するマイ以下のようなものです:私はこの中で行方不明です

public void saveQuoteImage(Bitmap quoteImage){ 
    Date now = new Date(); 
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); 
    try { 
    // image naming and path to include sd card appending name you choose for file 

    String dirPath=Environment.getExternalStorageDirectory().toString()+File.separator+"quotes_king"; 

    File dirFile=new File(dirPath); 
    if(!dirFile.exists())dirFile.mkdirs(); 

    // String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg"; 
    // create bitmap screen capture 
    File imageFile = new File(dirFile.getPath()+File.separator+ now + ".jpg"); 

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

    outputStream.flush(); 
    outputStream.close(); 

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

何?

ありがとうございました

+0

エラーは何ですか? –

+0

@victorsosaコメントありがとうございます。それは何のエラーも与えずに私の携帯電話を再起動した後に保存された画像を表示します。 –

答えて

4

あなたは自分のファイルが存在することをMediaScannerで伝える必要があります。

このような何か:

public final void addManually(File file) { 
     try{ 
      MediaStore.Images.Media.insertImage(mContext.getContentResolver(), 
        file.getAbsolutePath(), file.getName(), null); 
      mContext.sendBroadcast(new Intent(
        Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, 
        Uri.fromFile(file)) 
      ); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 
+0

これは正常に動作しています:) –

0

android.Justでの使用メディアスキャナは以下のコードを追加します。

MediaScannerConnection.scanFile(this, new String[] { imagefile.toString() }, null, null); 
関連する問題