2013-08-31 22 views
2

頭疼了一天之後被上傳,由於實施工作的照片上傳系統,我覺得在家裏舒展IM以前的問題。我的最後也是最後一步是允許我的用戶在剪切圖像後上傳圖像。位圖文件,可以通過異步

裁剪後發生我有機會獲得一個位圖,就是用使用bitmap.The異步請求的lib IM一個ImageView的是:http://loopj.com/android-async-http/

和使用API​​ IM是建立​​在這樣一種方式,我需要送過來「文件」,例如:

File myFile = new File("/path/to/file.png"); 
RequestParams params = new RequestParams(); 
try { 
    params.put("profile_picture", myFile); 
} catch(FileNotFoundException e) {} 

什麼是我對把我的位圖到「文件」

回答

0

選項試試這個:

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
         "/to"; 
File dir = new File(file_path); 
if(!dir.exists) 
dir.mkdirs(); 
File file = new File(dir, "file.png"); 
FileOutputStream fOut = new FileOutputStream(file); 

bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut); 
fOut.flush(); 
fOut.close(); 

,並在您的清單文件下面的權限。

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

您可以使用Bitmap.compress方法將位圖保存到文件。只要提供適當的FileOutputStream作爲參數。

您也可以上傳圖像,而無需使用的文件,只需將其保存到字節數組,然後上傳作爲數組。

ByteArrayOutputStream out = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 85, out); 
byte[] myByteArray = out.toByteArray(); 
RequestParams params = new RequestParams(); 
params.put("profile_picture", new ByteArrayInputStream(myByteArray), "image.png"); 
+0

好極了,非常感謝 – Eyeball

+1

供參考:PNG是一種無損格式,意味着85將被忽略。 – Takide