2012-11-03 13 views
2

我試圖將實際圖像從我的應用程序資源保存到SD卡上的文件中,以便消息傳遞應用程序可以訪問它但我得到的錯誤 - 類型FileOutputStream中的方法write(byte [])不適用於參數(Drawable) - 它應該保存它的地方。我相信我沒有使用合適的方法來保存實際圖像本身,而且我很難找到解決方案。FileOutputStream類型中的方法write(byte [])不適用於參數(可繪製)

     // Selected image id 
       int position = data.getExtras().getInt("id"); 
       ImageAdapter imageAdapter = new ImageAdapter(this); 
       ChosenImageView.setImageResource(imageAdapter.mThumbIds[position]); 
       Resources res = getResources(); 
       Drawable drawable = res.getDrawable(imageAdapter.mThumbIds[position]); //(imageAdapter.mThumbIds[position]) is the resource ID 
       try { 
        File file = new File(dataFile, FILENAME); 
        file.mkdirs(); 
        FileOutputStream fos = new FileOutputStream(file); 
        fos.write(drawable); // supposed to save the image here, getting the error at fos.write 
        fos.close(); 
       }catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       } 
      } 
     }  

回答

2

您誤差不解釋這一切,A fileOuputStream不能把被拉伸,它需要一個字節數組。

所以你需要從你的Drawable中獲取字節。

嘗試:

Drawable drawable = res.getDrawable(imageAdapter.mThumbIds[position]); 
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap(); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
byte[] bitmapdata = stream.toByteArray(); 

....

fos.write(bitmapdata); 

而當你在它,放在最後博克

+0

TYVM的fos.close()是迷路瞭如何轉換可繪製到字節。非常感激。 – user1409172

相關問題