Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android将位图上传到服务器时内存使用率高(100MB)_Android_Bitmap_Heap Memory - Fatal编程技术网

Android将位图上传到服务器时内存使用率高(100MB)

Android将位图上传到服务器时内存使用率高(100MB),android,bitmap,heap-memory,Android,Bitmap,Heap Memory,我试图解决我的问题,有时会导致OutOfMemoryException。我只是将位图上传到服务器,然后退出活动。我试着回收我的位图,但碎片还是很大。这是我的代码: private class UploadTask extends AsyncTask<Bitmap, Void, Void> { private ProgressDialog pDialog; @Override protected void onPreExecute() { s

我试图解决我的问题,有时会导致OutOfMemoryException。我只是将位图上传到服务器,然后退出活动。我试着回收我的位图,但碎片还是很大。这是我的代码:

private class UploadTask extends AsyncTask<Bitmap, Void, Void> {

    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(NewEvent.this);
        pDialog.setMessage("Uploading data...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();

    }


    protected Void doInBackground(Bitmap... bitmaps) {
        if (bitmaps[0] == null)
            return null;
        setProgress(0);

        Bitmap bitmap = bitmaps[0];
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); // convert Bitmap to ByteArrayOutputStream
        InputStream in = new ByteArrayInputStream(stream.toByteArray()); // convert ByteArrayOutputStream to ByteArrayInputStream
        bitmap.recycle();
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            HttpPost httppost = new HttpPost(
                    "my server"); // server

            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("fileToUpload",
                    System.currentTimeMillis() + ".jpg", in);
            httppost.setEntity(reqEntity);

            Log.i("TAG", "request " + httppost.getRequestLine());
            HttpResponse response = null;
            try {
                response = httpclient.execute(httppost);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if (response != null)
                    try {
                        response1 = EntityUtils.toString(response.getEntity());
                        response1 = response1.replace("\n", "").replace("\r", "");
                        Log.i("TAG", "response " + response1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

            } finally {

            }
        } finally {

        }

        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Void result) {

        super.onPostExecute(result);
        pDialog.dismiss();
        uploadJSON();
    }
}
private类UploadTask扩展了AsyncTask{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(NewEvent.this);
setMessage(“上传数据…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
受保护的Void doInBackground(位图…位图){
if(位图[0]==null)
返回null;
setProgress(0);
位图位图=位图[0];
ByteArrayOutputStream=新建ByteArrayOutputStream();
compress(bitmap.CompressFormat.PNG,100,stream);//将位图转换为ByteArrayOutputStream
InputStream in=new ByteArrayInputStream(stream.toByteArray());//将ByteArrayInputStream转换为ByteArrayInputStream
bitmap.recycle();
DefaultHttpClient httpclient=新的DefaultHttpClient();
试一试{
HttpPost HttpPost=新的HttpPost(
“我的服务器”);//服务器
MultipartEntity reqEntity=新的MultipartEntity();
requentity.addPart(“fileToUpload”,
System.currentTimeMillis()+“.jpg”,in);
httppost.setEntity(reqEntity);
Log.i(“TAG”、“request”+httppost.getRequestLine());
HttpResponse响应=null;
试一试{
response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
if(响应!=null)
试一试{
response1=EntityUtils.toString(response.getEntity());
response1=response1.replace(“\n”和“).replace(“\r”和“);
Log.i(“标记”、“响应”+response1);
}捕获(IOE异常){
e、 printStackTrace();
}
}最后{
}
}最后{
}
if(in!=null){
试一试{
in.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
if(流!=null){
试一试{
stream.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
返回null;
}
@凌驾
受保护的void onProgressUpdate(void…值){
//TODO自动生成的方法存根
super.onProgressUpdate(值);
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
pDialog.disclose();
上传JSON();
}
}

位图。压缩可能导致OOM错误。尝试使用BitmapFactory.decodeStreamBitmapFactory.Options

BitmapFactory.Options options = new BitmapFactory.Options();
options.outHeight=400;
options.outWidth=600;
BitmapFactory.decodeStream(inputStream, null, options);

你能发一段代码片段让我明白吗?我编辑了我的答案。有很多选择。选择适合您的选项。(options.inSampleSize适合开始尝试)OP仍然需要
位图。compress
。如果OP真的从FileInputStream读取数据(他没有说太多),这仍然是一个糟糕的解决方案。@greenapps你说得对,但在缩放原始图像后,bitmap.compress不应引起OOM错误。
将位图上载到服务器
。请告诉我这个位图来自哪里。你不想上传一个文件吗?您没有将位图上载到服务器。您正在将PNG图像上载到服务器。你告诉你的服务器是一个.jpg。我的位图来自ImageView,我从camera或Gallery发布它。在这两种情况下,你可以在ImageView中加载一个jpg文件作为位图。然后再从ImageView中提取位图?当然,如果您从Gallery中选择现有文件,最好使用该文件。如果你要上传,你最好上传那个文件。让生活更轻松。当然没有内存不足的错误。如何从imageview中提取位图?我同意@greenapps。从位图中提取图像在您的情况下毫无意义。我发现这是一个简单的解决方案。现在我面临另一个问题,那就是数据有时是空的。(主要发生在Nexus上)