0

我有一个线程,带有一个 for 循环,它通过文件路径对象到画廊中的图像。线程内部的代码是:

    for(int i=0;i<tellerList;i++){
        Log.e("Picture", x.get(i));


        TAG_PICTURE = x.get(i);

        Bitmap bitmap = BitmapFactory.decodeFile(TAG_PICTURE);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        double hoyde1 = bitmap.getHeight();
        double bredde1 = bitmap.getWidth();
        int hoyde = (int) (hoyde1 / 1.5);
        int bredde = (int) (bredde1 / 1.5);
        Bitmap bitmapfinal = Bitmap.createScaledBitmap(bitmap, bredde,hoyde, false);
        bitmapfinal.compress(Bitmap.CompressFormat.JPEG, 80, stream);
        byte[] byte_arr = stream.toByteArray();
        try {
            stream.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String image_str = Base64.encodeToString(byte_arr,
                Base64.DEFAULT);

        // HTTP POST

        ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
        param.add(new BasicNameValuePair("tag", login_tag));
        param.add(new BasicNameValuePair("unique_id", in
                .getStringExtra(TAG_UID)));
        param.add(new BasicNameValuePair("picture_data", image_str));

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(param));

            Log.e("Ferdig med å laste opp", "Ferdig");
        } catch (Exception e) {

            Log.e("log_tag", "Error in http connection " + e.toString());
        }

    }

代码执行。第一张图片已成功上传,但当线程再次尝试运行时,它崩溃了。LogCat 输出为:

01-06 18:14:47.113: E/dalvikvm(5010): Out of memory: Heap Size=58531KB, Allocated=50243KB, Limit=65536KB
01-06 18:14:47.113: E/dalvikvm(5010): Extra info: Footprint=56483KB, Allowed Footprint=58531KB, Trimmed=3712KB
01-06 18:14:47.113: D/skia(5010): --- decoder->decode returned false
01-06 18:14:47.113: W/dalvikvm(5010): threadid=47: thread exiting with uncaught exception (group=0x40d782d0)
01-06 18:14:47.113: E/AndroidRuntime(5010): FATAL EXCEPTION: Thread-543
01-06 18:14:47.113: E/AndroidRuntime(5010): java.lang.OutOfMemoryError: (Heap Size=58531KB, Allocated=50244KB)
01-06 18:14:47.113: E/AndroidRuntime(5010):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
01-06 18:14:47.113: E/AndroidRuntime(5010):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:658)
01-06 18:14:47.113: E/AndroidRuntime(5010):     at 
android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:347)
01-06 18:14:47.113: E/AndroidRuntime(5010):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:430)
01-06 18:14:47.113: E/AndroidRuntime(5010):     at com.example.socializerfragments.PhotoUploadSelecter.run(PhotoUploadSelecter.java:96)
01-06 18:14:47.113: E/AndroidRuntime(5010):     at java.lang.Thread.run(Thread.java:864)

所以一张图片很有效,不止一张,不是很多......有谁知道问题的解决方案可能是什么?

谢谢!

4

3 回答 3

0

问题BitmapFactory.decodeFile(BitmapFactory.java:347)如 logcat 中所述。有几个解决方案。首先是捕获内存不足,请参阅此帖子了解更多信息。您还可以减小位图的大小。可以在文档中找到一个很好的示例

于 2013-01-06T17:50:09.290 回答
0

您可以使用 ByteArrayInputStream(stringVariable.getBytes(charset)) InputStream is = new ByteArrayInputStream(stringVariable.getBytes(StandardCharsets.UTF_8)); 将其转换为 InputStream

于 2015-04-17T16:43:21.057 回答
0
Bitmap FixBitmap;
String ImagePath = "image_path" ;
String ConvertImage ;
byte[] byteArray ;
ByteArrayOutputStream byteArrayOutputStream ;

然后你必须检查

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_FROM_FILE && resultCode == RESULT_OK && null != data)
    {

        Uri uri = data.getData();

        String[] prjection ={MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(uri,prjection,null,null,null);

        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(prjection[0]);

        ImagePath = cursor.getString(columnIndex);

        cursor.close();

        FixBitmap = BitmapFactory.decodeFile(ImagePath);

        ShowSelectedImage = (ImageView)findViewById(R.id.imageView);

        ShowSelectedImage.setImageBitmap(BitmapFactory.decodeFile(ImagePath));


    }
}

这也是,请

@Override
        protected String doInBackground(Void... params) {


            FixBitmap = BitmapFactory.decodeFile(ImagePath);
            //FixBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gv); // this code will upload the pic to server that in drawable place 

            byteArrayOutputStream = new ByteArrayOutputStream();
           FixBitmap.compress(Bitmap.CompressFormat.JPEG, 60, byteArrayOutputStream); //compress to 50% of original image quality
           byteArray = byteArrayOutputStream.toByteArray();

           ConvertImage = Base64.encodeToString(byteArray, Base64.DEFAULT);

** 不要添加此代码

httpURLConnection.setRequestProperty("Content-Type", "image/jpeg");
于 2017-07-16T04:38:36.680 回答