2012-11-01 169 views
4

我有一個位圖圖像,我必須將其存儲在SD卡的文件夾中,我的代碼如下所示。它按預期創建文件夾和文件,但圖像不存儲在文件中,它仍然是一個空文件...任何人都可以告訴我什麼是錯的?將位圖保存到SD卡

Bitmap merged = Bitmap.createBitmap(mDragLayer.getChildAt(0).getWidth(), mDragLayer.getChildAt(0).getHeight(), Bitmap.Config.ARGB_8888); 
Canvas c = new Canvas(merged); 

// save to folder in sd card 
try { 
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "folder"); 
    if(!imagesFolder.exists()) 
     imagesFolder.mkdirs(); 
    int imageNum; 
    if(imagesFolder.list()==null) 
     imageNum = 1; 
    else 
     imageNum = imagesFolder.list().length + 1; 

    String fileName = "file_" + String.valueOf(imageNum) + ".jpg"; 
    File output = new File(imagesFolder, fileName); 
    while(output.exists()){ 
     imageNum++; 
     fileName = "file_" + String.valueOf(imageNum) + ".jpg"; 
     output = new File(imagesFolder, fileName); 
    } 

    OutputStream fOut = new FileOutputStream(output); 
    merged.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 
    fOut.flush(); 
    fOut.close(); 

    Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show(); 

    } catch (FileNotFoundException e) { 
    e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
} 

回答

17

首先添加權限的AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 

然後在Java文件如下寫下來。

String extr = Environment.getExternalStorageDirectory().toString(); 
      File mFolder = new File(extr + "/MyApp"); 

      if (!mFolder.exists()) { 
       mFolder.mkdir(); 
      } 

      String strF = mFolder.getAbsolutePath(); 
      File mSubFolder = new File(strF + "/MyApp-SubFolder"); 

      if (!mSubFolder.exists()) { 
       mSubFolder.mkdir(); 
      } 

      String s = "myfile.png"; 

      f = new File(mSubFolder.getAbsolutePath(),s); 

更新

  String strMyImagePath = f.getAbsolutePath(); 
      FileOutputStream fos = null; 
      try { 
       fos = new FileOutputStream(f); 
       bitmap.compress(Bitmap.CompressFormat.PNG,70, fos); 

       fos.flush(); 
       fos.close(); 
       // MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen"); 
      }catch (FileNotFoundException e) { 

       e.printStackTrace(); 
      } catch (Exception e) { 

       e.printStackTrace(); 
      } 
+0

我在哪裏可以放置實際的位圖? – jpmastermind

+0

看到我的更新代碼 –

+0

謝謝,它現在的工作:) – jpmastermind

9

你只需要一個位圖

,你必須通過一個路徑來存儲圖像

Bitmap b = pagesView.getDrawingCache(); 
b.compress(CompressFormat.JPEG, 100, new FileOutputStream(Environment.getExternalStorageDirectory() + "/NameOfFile.jpg")); 

,你必須添加權限在清單文件中。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

+0

我試過這個,我仍然得到一個空的0byte jpeg文件 – jpmastermind

6

不要讓複雜的代碼難以實現它真的很簡單請嘗試下面的代碼。

在你的SD卡創建第一個目錄:

public static String strpath = android.os.Environment.getExternalStorageDirectory().toString(); 
public static String dirName = "DIR_NAME"; 

File makeDirectory = new File(strpath+"/"+dirName); 
makeDirectory.mkdir(); 

那麼你應該做兩個字符串瓦爾象下面這樣:

String filename = "yourImageName".jpg"; 
String dirpath =strpath + "/"+dirName + "/"; 

make文件變量:

File storagePath = new File(dirpath); 
File myImage = new File(storagePath, filename); 
outStream = new FileOutputStream(myImage); 
outStream.write(data); 
outStream.close(); 

但願這對你有幫助。