經歷了很多內存分配錯誤解決方案之後,我暗示並嘗試了很多針對我的應用程序的應用程序,但我不知道它爲什麼仍然崩潰內存。它只發生在相機文件夾圖像不適用於其他人。請幫我糾正我的代碼,如果它在某個地方是錯誤的。 第一個是在活動結果使用圖像意圖選擇圖像之後:內存不足在將位圖轉換爲字節數組時,攝像機圖像出現錯誤
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//getting image from gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting image to ImageView
ivImage.setImageBitmap(bitmap);
Uri selectedImageUri = data.getData();
} catch (Exception e) {
e.printStackTrace();
}
}
另外還配有我在哪裏通過轉換位圖來字節數組上傳圖像服務器的方法:
public void upload()
{
progressDialog = new ProgressDialog(AddPropertyThird.this);
progressDialog.setMessage("Uploading, please wait...");
progressDialog.show();
//converting image to base64 string
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = true;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageBytes = stream.toByteArray();
bitmap.recycle();
final String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
//sending image to server
StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("volleyerror", "" + volleyError);
}
}) {
//adding parameters to send
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("ImgStr", imageString);
return parameters;
}
};
RequestQueue rQueue = Volley.newRequestQueue(AddPropertyThird.this);
rQueue.add(request);
}
這是我的logcat錯誤:
E/dalvikvm-heap: Out of memory on a 4192272-byte allocation.
Out of memory on a 4186240-byte allocation.
Out of memory on a 2089032-byte allocation.
FATAL EXCEPTION: main
java.lang.OutOfMemoryError
java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122)
http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object ...希望這會幫助你。 –
[將圖像加載到位圖對象時出現內存不足的問題]的可能重複(http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-位圖對象) –
我已經通過了推薦的鏈接。它沒有解決問題@JaiminThakkar – user1111