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)

つまり、1つの画像がすりおろし、複数の画像が機能しますが、それほど多くはありません...問題の解決策が何か知っている人はいますか?

ありがとう!

4

3 に答える 3

0

問題はBitmapFactory.decodeFile(BitmapFactory.java:347)logcatに記載されているとおりです。いくつかの解決策があります。まず、メモリ不足をキャッチします。詳細については、この投稿を参照してください。ビットマップのサイズを小さくすることもできます。優れたサンプルはドキュメントにあります

于 2013-01-06T17:50:09.290 に答える
0

ByteArrayInputStream(stringVariable.getBytes(charset))を使用してInputStreamに変換できます。InputStreamis = new ByteArrayInputStream(stringVariable.getBytes(StandardCharsets.UTF_8));

于 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 に答える