2016-04-14 12 views
0

leider die lösungen, die ich gefunden habe, funktionierte nicht auf android 5.1.1. Ich habe eine Bitmap namens Quelle. Ich muss es direkt in der Galerie meines Telefons speichern. Mein Manifest enthält <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> Können Sie mir eine Arbeitsmethode dafür geben?wie man bitmap in android gallery speichern

+1

@Mihalil können Sie Ihre Lösung hier veröffentlichen? – dex

+0

@Mihalil Чёткая ава –

Antwort

9

verwenden ein:

private void saveImage(Bitmap finalBitmap, String image_name) { 

     String root = Environment.getExternalStorageDirectory().toString(); 
     File myDir = new File(root); 
     myDir.mkdirs(); 
     String fname = "Image-" + image_name+ ".jpg"; 
     File file = new File(myDir, fname); 
     if (file.exists()) file.delete(); 
     Log.i("LOAD", root + fname); 
     try { 
      FileOutputStream out = new FileOutputStream(file); 
      finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
      out.flush(); 
      out.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
0

Tun Sie es in einer Zeile MediaStore.Images.Media.insertImage(applicationContext.getContentResolver(), IMAGE ,"nameofimage" , "description");

0

Verwenden Sie diesen Code, um dies wir Ihnen helfen einen bestimmten Ordner zu speichern Bilder in die saved_images und dass Ordner Bilder zeigen in der Galerie ist sofort.

private void SaveImage(Bitmap finalBitmap) { 

String root = Environment.getExternalStoragePublicDirectory(
    Environment.DIRECTORY_PICTURES).toString(); 
File myDir = new File(root + "/saved_images"); 
myDir.mkdirs(); 
Random generator = new Random(); 

int n = 10000; 
n = generator.nextInt(n); 
String fname = "Image-"+ n +".jpg"; 
File file = new File (myDir, fname); 
if (file.exists()) file.delete(); 
try { 
    FileOutputStream out = new FileOutputStream(file); 
    finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
    // sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
    //  Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
    out.flush(); 
    out.close(); 

} catch (Exception e) { 
    e.printStackTrace(); 
} 
// Tell the media scanner about the new file so that it is 
// immediately available to the user. 
MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null, 
    new MediaScannerConnection.OnScanCompletedListener() { 
     public void onScanCompleted(String path, Uri uri) { 
      Log.i("ExternalStorage", "Scanned " + path + ":"); 
      Log.i("ExternalStorage", "-> uri=" + uri); 
     } 
    }); 
} 
Verwandte Themen