Java 将照片发布到android数据库时内存不足
我正试图将截图作为Java 将照片发布到android数据库时内存不足,java,android,imageview,bytearray,android-volley,Java,Android,Imageview,Bytearray,Android Volley,我正试图将截图作为字节[]转换为字符串发送到我的数据库。这就是我调用截取方法并从ImageView中的图像中获取字符串的地方: addPic.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ImageView pic_holder = (ImageView) findViewById(R.id.picturedis
字节[]
转换为字符串
发送到我的数据库。这就是我调用截取方法并从ImageView
中的图像中获取字符串的地方:
addPic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView pic_holder = (ImageView) findViewById(R.id.picturedisplay);
Bitmap bitmap = ((BitmapDrawable)pic_holder.getDrawable()).getBitmap();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();
String img_str = Base64.encodeToString(image, 0);
MyLocation myLocation = new MyLocation();
myLocation.getLocation(getApplicationContext(), locationResult);
boolean r = myLocation.getLocation(getApplicationContext(),
locationResult);
if (!r) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(getApplicationContext());
builder1.setMessage("Please enable your GPS.");
builder1.setCancelable(false);
builder1.setPositiveButton(
"Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
else {
savePhoto(Latitude,Longitude,username,picText,img_str);
}
这是我截击的方法:
public void savePhoto(final Double latitude, final Double longitude, final String username, final String description,final String picture ) {
pDialog.setMessage("Posting picture..");
pDialog.show();
request = new StringRequest(Request.Method.POST, SL_URL, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
pDialog.dismiss();
Toast.makeText(getApplicationContext(),
"Your picture has been posted.", Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("latitude", Double.toString(latitude));
hashMap.put("longitude", Double.toString(longitude));
hashMap.put("image",picture);
hashMap.put("user", username);
hashMap.put("description", description);
return hashMap;
}
};
queue.add(request);
}
使用位图时要非常小心,因为它们非常大,因此会出现此错误。
在清单文件中的应用程序标记中使用android:largeHeap=“true”
对我来说很有效,因为您最初有更多的空间,但这仍然无法100%解决问题,所以只需尝试最小化内存使用,创建位图,使用它,然后将其删除,因此,尽量不要将它们存储到列表或地图中。您建议我如何将照片发送到数据库?它被发送到服务器上的一个php脚本,该脚本将其插入mysqli数据库我会说如果可以,然后立即发送,如果您没有访问权限(没有互联网或其他东西),则将其存储到SQLite,从而存储在电话数据库中,然后当您再次连接互联网时,检查本地数据库中的内容并将其发送到服务器,因此,如果图像来自设备存储器,则不会将它们放入内存。您可以将图像作为文件发送到服务器。使用loopj asynchttpclient库而不是volley。非常简单。@SreeAndroidDev基本上我用相机拍摄照片,在屏幕上显示在imageview中,用户可以选择将照片存储在手机中,但不必这样做,然后当他按下发送按钮时,照片被发送到服务器,他也可以选择删除照片,照片不会存储在任何地方。这是我想要实现的行为,因此,如果他不选择将其保存在手机上,我就无法从存储器中真正获取它。好的,这样一来,您的存储器中一次只有一个图像,因此您应该很好,无需存储任何内容。您需要将图像缩小一些或将图像分块发送(我不知道volley是否支持)你能给我一个如何缩小图像比例的例子吗?我现在设法让它发送,但是我收到了这个错误com.android.volley.NoConnectionError:java.net.SocketException:sendto失败:EPIPE(断管)
02-22 21:33:54.350 5233-5240/citylife.com.city W/art: Suspending all threads took: 17.926ms
02-22 21:33:58.310 5233-5233/citylife.com.city I/Choreographer: Skipped 707 frames! The application may be doing too much work on its main thread.
02-22 21:33:59.090 5233-5233/citylife.com.city D/DisplayManager: DisplayManager()
02-22 21:34:01.860 5233-5240/citylife.com.city W/art: Suspending all threads took: 11.464ms
02-22 21:34:05.360 5233-5240/citylife.com.city W/art: Suspending all threads took: 6.127ms
02-22 21:34:06.350 5233-5248/citylife.com.city W/art: Suspending all threads took: 100.142ms
02-22 21:34:06.390 5233-5240/citylife.com.city W/art: Suspending all threads took: 32.192ms
02-22 21:34:06.410 5233-5276/citylife.com.city I/art: Alloc partial concurrent mark sweep GC freed 16(496B) AllocSpace objects, 1(25MB) LOS objects, 11% free, 60MB/68MB, paused 962us total 53.527ms
02-22 21:34:06.410 5233-5248/citylife.com.city I/art: WaitForGcToComplete blocked for 53.981ms for cause Background
02-22 21:34:06.490 5233-5276/citylife.com.city I/art: WaitForGcToComplete blocked for 9.452ms for cause Alloc
02-22 21:34:06.500 5233-5276/citylife.com.city I/art: Alloc sticky concurrent mark sweep GC freed 1(32B) AllocSpace objects, 0(0B) LOS objects, 9% free, 79MB/87MB, paused 1.008ms total 7.842ms
02-22 21:34:06.530 5233-5276/citylife.com.city I/art: Alloc partial concurrent mark sweep GC freed 8(256B) AllocSpace objects, 1(18MB) LOS objects, 11% free, 60MB/68MB, paused 1.099ms total 26.534ms
02-22 21:34:06.570 5233-5248/citylife.com.city W/art: Suspending all threads took: 39.015ms
02-22 21:34:06.610 5233-5276/citylife.com.city I/art: Alloc partial concurrent mark sweep GC freed 7(224B) AllocSpace objects, 1(18MB) LOS objects, 10% free, 70MB/78MB, paused 940us total 29.766ms
02-22 21:34:06.690 5233-5276/citylife.com.city I/art: Clamp target GC heap from 96MB to 96MB
02-22 21:34:06.690 5233-5276/citylife.com.city I/art: Alloc partial concurrent mark sweep GC freed 6(192B) AllocSpace objects, 0(0B) LOS objects, 7% free, 88MB/96MB, paused 1.176ms total 18.263ms
02-22 21:34:06.700 5233-5276/citylife.com.city I/art: Alloc sticky concurrent mark sweep GC freed 2(64B) AllocSpace objects, 0(0B) LOS objects, 7% free, 88MB/96MB, paused 956us total 7.052ms
02-22 21:34:06.750 5233-5276/citylife.com.city I/art: Clamp target GC heap from 96MB to 96MB
02-22 21:34:06.750 5233-5276/citylife.com.city I/art: Alloc concurrent mark sweep GC freed 12(12KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 88MB/96MB, paused 972us total 50.996ms
02-22 21:34:06.750 5233-5276/citylife.com.city I/art: Forcing collection of SoftReferences for 9MB allocation
02-22 21:34:06.780 5233-5276/citylife.com.city I/art: Clamp target GC heap from 96MB to 96MB
02-22 21:34:06.780 5233-5276/citylife.com.city I/art: Alloc concurrent mark sweep GC freed 13(408B) AllocSpace objects, 0(0B) LOS objects, 7% free, 88MB/96MB, paused 1.060ms total 28.205ms
02-22 21:34:06.780 5233-5276/citylife.com.city E/art: Throwing OutOfMemoryError "Failed to allocate a 9606684 byte allocation with 8049319 free bytes and 7MB until OOM"
02-22 21:34:06.800 5233-5276/citylife.com.city E/AndroidRuntime: FATAL EXCEPTION: Thread-3159
Process: citylife.com.city, PID: 5233
java.lang.OutOfMemoryError: Failed to allocate a 9606684 byte allocation with 8049319 free bytes and 7MB until OOM
at java.nio.charset.Charsets.toUtf8Bytes(Native Method)
at java.lang.String.getBytes(String.java:775)
at java.lang.String.getBytes(String.java:759)
at com.android.volley.Request.encodeParameters(Request.java:452)
at com.android.volley.Request.getBody(Request.java:435)
at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:236)
at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)