2017-01-11 13 views
0

ボタン内のアンドロイドギャラリーに自分のアプリケーションからビットマップを保存する方法を知りましたか?私はちょうど私のビットマップがギャラリーに置かれて、ユーザーが彼のバックグラウンドとして設定することができるようにしたい。だからどうしたの?ギャラリーに画像を保存

+1

おそらく、ギャラリーが画像を読み込むデバイスの場所に画像を保存してみてください。たとえば、DCIMなどです。 –

+0

ギャラリーはアプリであり、場所ではありません。 [外部ストレージ](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html)のような場所に画像を保存します。 – CommonsWare

+0

さて、どうすれば対応できますか?私はたくさんのバージョンを試しましたが、何も私のために働いていますか? – Luca486

答えて

0

あなたは、あなたがこのようなギャラリーに画像を保存することができます

private void takeSnapShot(Bitmap bitmap) { 
    OutputStream outStream = null; 
    String state = Environment.getExternalStorageState(); 
    String appName = mContext.getString(R.string.app_name); 

    if (state.equals(Environment.MEDIA_MOUNTED)) { 
     String sdroot = Environment.getExternalStorageDirectory().getAbsolutePath(); 
     String path = sdroot + "/" + appName; 
     File dir = new File(path); 
     if (!dir.exists() && !dir.mkdirs()) { 
      return; 
     } 

     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US); 
     String dstr = sdf.format(new Date()); 
     String filename = path + "/" + dstr + ".jpg"; 

     try { 
      outStream = new FileOutputStream(filename); 
     } catch (FileNotFoundException e) { 
      LogUtils.println("Exception while writing image"); 
      e.printStackTrace(); 
     } 

     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream); 

     try { 
      if (outStream != null) 
       outStream.close(); 
      mMediaScanner.scanMediaByName(filename, R.string.snapshot_save); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


    } else { 
     // Add some parameters to the image that will be stored in the Image ContentProvider 
     int UNIQUE_BUCKET_ID = 111; 
     String disp = "anything"; 

     ContentValues values = new ContentValues(7); 
     values.put(MediaStore.Images.Media.DISPLAY_NAME, disp); 
     values.put(MediaStore.Images.Media.TITLE, disp); 
     values.put(MediaStore.Images.Media.DESCRIPTION, appName + disp); 
     values.put(MediaStore.Images.Media.BUCKET_DISPLAY_NAME, appName + " snapshot"); 
     values.put(MediaStore.Images.Media.BUCKET_ID, UNIQUE_BUCKET_ID); 
     values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()); 
     values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 
     // Inserting the image meta data inside the content provider 
     Uri uri = mContext.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 

     try { 
      if (uri != null) { 
       outStream = mContext.getContentResolver().openOutputStream(uri); 
       if (outStream != null) { 
        outStream.close(); 
       } 
      } 
     } catch (FileNotFoundException e) { 
      LogUtils.println("Exception while writing image"); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
0

あなたのニーズに合った以下の機能を変更します。

MediaStore.Images.Media.insertImage(getContentResolver()、yourBitmap、yourTitle、yourDescription);同様に

試しこの

画像がメディアストアプロバイダ

パブリック静的ボイドaddImageToGallery(最終列filePathに、最終的なコンテキストコンテキスト){

ContentValues values = new ContentValues(); 

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

context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values); 
に格納されているギャラリーに表示されています

}

+0

これを保存するだけで簡単に聞こえる – Luca486

関連する問題