2013-03-15 8 views
14

Mam problem z zapisaniem bitmap do plików. Moja metoda jest tak:Zapisz bitmapę w pliku i zwróć Plik z obrazem bitmapowym

private File savebitmap(Bitmap bmp) { 
    String extStorageDirectory = Environment.getExternalStorageDirectory() 
      .toString(); 
    OutputStream outStream = null; 

    File file = new File(bmp + ".png"); 
    if (file.exists()) { 
     file.delete(); 
     file = new File(extStorageDirectory, bmp + ".png"); 
     Log.e("file exist", "" + file + ",Bitmap= " + bmp); 
    } 
    try { 
     outStream = new FileOutputStream(file); 
     bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
     outStream.flush(); 
     outStream.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    Log.e("file", "" + file); 
    return file; 

} 

Daje mi błąd z file.I wzywam tę metodę tak:

Drawable d = iv.getDrawable(); 
Bitmap bitmap = ((BitmapDrawable) d).getBitmap(); 
File file = savebitmap(bitmap); 

Proszę mi pomóc ...

+1

Jakie jest znaczenie tej linii pliku file = new File (bmp +”. png ") ;? –

+1

zdefiniuj "błąd pliku" (np. Post stacktrace) – njzk2

+1

@FestusTamakloe Przypuszczam, że fałszywie zakłada, że ​​'bmp.toString()' zwróci 'nazwę' bmp, –

Odpowiedz

32

staram się dokonać pewnych korekt na kodzie Zakładam, że chcesz użyć nazwy pliku zamiast bitmapy jako parametr

private File savebitmap(String filename) { 
     String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
     OutputStream outStream = null; 

     File file = new File(filename + ".png"); 
     if (file.exists()) { 
     file.delete(); 
     file = new File(extStorageDirectory, filename + ".png"); 
     Log.e("file exist", "" + file + ",Bitmap= " + filename); 
     } 
     try { 
     // make a new bitmap from your file 
     Bitmap bitmap = BitmapFactory.decodeFile(file.getName()); 

     outStream = new FileOutputStream(file); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
     outStream.flush(); 
     outStream.close(); 
     } catch (Exception e) { 
     e.printStackTrace(); 
     } 
     Log.e("file", "" + file); 
     return file; 

    } 
+0

Dzięki .. To działa .. !!! – AndiM

+0

@Meghs nie ma za co. Jeśli to pomoże, a następnie głosuj w górę. Dzięki –

2

Nie możesz pisać jak to

File file = new File(bmp + ".png"); 

i linia ta jest również źle

file = new File(extStorageDirectory, bmp + ".png"); 

Musisz podać wartość string, a nie bitmapę.

File file = new File(filename + ".png"); 
0

Zmień plik file = new File (bmp + ".png"); do Plik file = nowy Plik (extStorageDirectory, "bmp.png"); jak zrobiłeś prawie po raz drugi.

Powiązane problemy