Java OutofMemory Android位图
我需要将我所有的图片从手机上传到服务器,但问题是我的异步内存不足 我看到很多解决方案,他们要求用户将图像缩小,但我需要发送原始图像,而不是压缩或修改 我试着穿线。睡眠(5000) 我还尝试了以下操作,但问题是它在上传位图之前正在清理我的位图,因为它是异步的 bm.recycle(); bm=null 我可以一张一张地上传图片吗Java OutofMemory Android位图,java,android,bitmap,Java,Android,Bitmap,我需要将我所有的图片从手机上传到服务器,但问题是我的异步内存不足 我看到很多解决方案,他们要求用户将图像缩小,但我需要发送原始图像,而不是压缩或修改 我试着穿线。睡眠(5000) 我还尝试了以下操作,但问题是它在上传位图之前正在清理我的位图,因为它是异步的 bm.recycle(); bm=null 我可以一张一张地上传图片吗 @Override protected void onCreate(Bundle savedInstanceState) { pic
@Override
protected void onCreate(Bundle savedInstanceState) {
picturePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()+"/Camera/";
Log.d("Files", "Path: " + picturePath);
File f = new File(picturePath);
File file[] = f.listFiles();
Log.d("Files", "Size: " + file.length);
for (int i=0; i < file.length; i++)
{
Log.d("Files", "FileName:" + file[i].getName());
// Image location URL
Log.e("Files", "----------------" + picturePath+file[i].getName());
// Image
Bitmap bm = BitmapFactory.decodeFile(picturePath + file[i].getName());
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 75, bao);
byte[] ba = bao.toByteArray();
ba1 = Base64.encodeToString(ba, Base64.DEFAULT);
Log.d("Files", "Uploading");
new uploadToServer().execute();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class uploadToServer extends AsyncTask<Void, Void, String> {
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("base64", ba1));
nameValuePairs.add(new BasicNameValuePair("ImageName", System.currentTimeMillis() + ".jpg"));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String st = EntityUtils.toString(response.getEntity());
Log.v("Files", "In the try Loop" + st);
} catch (Exception e) {
Log.v("Files", "Error in http connection " + e.toString());
}
return "Success";
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
picturePath=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()+“/Camera/”;
Log.d(“文件”,“路径:+picturePath”);
文件f=新文件(picturePath);
File File[]=f.listFiles();
Log.d(“文件”,“大小:”+文件.length);
for(int i=0;i
您需要使用HttpUrlConnection的发送数据块。setChunkedStreamingMode(1024)代码>
查看此示例哪一行导致OutOfMemory?您正在创建一个名为“bm”的位图,但没有对其进行任何操作。。。你基本上上传的是一个空字节数组…你根本不应该解码位图-你想上传一个文件,而不是显示一个图像。使用BufferedInputStream将FileInputStream与目标文件包装在一起-read(byte[]buffer,int byteOffset,int byteCount)方法允许您分块写入websocket。@GilMoshayof My bad,Edited看看这个问题