在我的應用程序中,我顯示的圖像來自廚房和圖像選擇我想將圖像上傳到Web服務器。要將圖像上傳到服務器,我使用以下代碼,但我得到錯誤在 bitmapImage = BitmapFactory.decodeFile(path,opt);java.lang.outofmemoryerror位圖大小在位圖上超過vm預算
private void uploadImage(String selectedImagePath) {
String str = null;
byte[] data = null;
String Responce= null;
Bitmap bitmap2 = null;
try {
File file=new File(selectedImagePath);
//FileInputStream fileInputStream = new FileInputStream(new File(imagePath2));
FileInputStream fileInputStream=new FileInputStream(selectedImagePath);
Log.i("Image path 2",""+selectedImagePath+"\n"+fileInputStream);
name=file.getName();
name=name.replace(".jpg","");
name=name.concat(sDate).concat(".jpg");
Log.e("debug",""+name);
//else
//{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];
//bitmapImage = BitmapFactory.decodeFile(path,opt);
bitmap2=BitmapFactory.decodeFileDescriptor(fd, outPadding, opts)
Log.i("Bitmap",""+bitmap.toString());
BitmapFactory.decodeStream(fileInputStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap2.compress(Bitmap.CompressFormat.PNG, 100, baos);
data = baos.toByteArray();
str=Base64.encodeBytes(data);
//}
//String image=str.concat(sDate);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",str));
nameValuePairs.add(new BasicNameValuePair("imagename", name));
Log.e("debug",""+nameValuePairs.toString());
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost("http://ufindfish.b4live.com/uploadTipImage.php");
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse=client.execute(post);
HttpEntity entity=httpResponse.getEntity();
InputStream inputStream=entity.getContent();
StringBuffer builder=new StringBuffer();
int ch;
while((ch = inputStream.read()) != -1)
{
builder.append((char)ch);
}
String s=builder.toString();
Log.i("Response",""+s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bitmap2!=null)
{
bitmap2.recycle();
}
@abhi ...謝謝你的回覆..但我仍然得到錯誤..1小問題...獲取位圖後,即**位圖位圖=位圖Factory.decode文件(文件路徑,o2); **在此之後,如何將其轉換爲基本64字符串...我使用下面的代碼爲這個bitmap.compress(Bitmap.CompressFormat.PNG,100,baos); \t \t \t \t data = baos.toByteArray(); \t \t \t \t str = Base64.encodeBytes(data); – android
@abhi ...謝謝我已經整理出這個問題...現在它的工作正常...非常感謝你... – android
@Abhi我用你的代碼,它解決了幾乎我的錯誤,謝謝你,但我面對一個由於這種方法的問題。這種方法正在旋轉我的原始圖像。而我不想要。我想顯示我的圖像,如在圖庫中顯示。有沒有辦法做到這一點? – anddev