2012-03-19 43 views
2

這是我的代碼從畫廊或相機拍攝照片。我已經實現了後臺線程來完成任務,然後使用SmartImageView使用url設置圖像。我的錯誤是內存分配的:在Android中使用線程進行圖像加載時出現內存不足錯誤。

upload.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View v) { 





          AlertDialog.Builder builder = new AlertDialog.Builder(activity.this); 
           builder.setMessage("Select") .setCancelable(false).setPositiveButton("Gallery", new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dialog, int id) { 
              Intent gallIntent=new Intent(Intent.ACTION_GET_CONTENT); 
              gallIntent.setType("image/*"); 
              startActivityForResult(gallIntent, 10); 
             } 
           }) 

           .setNegativeButton("Camera", new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dialog, int id) { 
              Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
              startActivityForResult(cameraIntent, 0); 
             } 
            }); 
          AlertDialog alert = builder.create(); 
          alert.show(); 




        if (bitmap == null) { 
         Toast.makeText(getApplicationContext(), 
           "Please select image", Toast.LENGTH_SHORT).show(); 
        } else { 
         dialog = ProgressDialog.show(activity.this, "Uploading", 
           "Please wait...", true); 
         //new ImageUploadTask().execute(); 
        } 
       } 
      }); 



    } 

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


      super.onActivityResult(requestCode, resultCode, data); 






       switch (requestCode) { 
       case 10: 
        if (resultCode == Activity.RESULT_OK) { 


          Thread t = new Thread() 
            { 


           public void run(){ 
         Uri imageUri = data.getData(); 
        Bitmap b; 
        try { 
         b = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri); 

         String timestamp = Long.toString(System.currentTimeMillis()); 
          MediaStore.Images.Media.insertImage(getContentResolver(), b, timestamp, timestamp); 
         HttpResponse httpResponse; 
         ByteArrayOutputStream bao = new ByteArrayOutputStream(); 

        b.compress(Bitmap.CompressFormat.JPEG, 100, bao); 

        byte [] ba = bao.toByteArray(); 
        int f = 0; 
        String ba1=Base64.encodeToString(ba, f); 



         try { 
          OAuth oAuth = new OAuth(activity.this); 
          HttpPost httpPost = new HttpPost("url"); 


          httpPost.setEntity(new ByteArrayEntity(ba)); 
          HttpClient httpClient= new DefaultHttpClient(); 
          httpResponse = httpClient.execute(httpPost); 
          int responseCode = httpResponse.getStatusLine().getStatusCode(); 
         } catch (ClientProtocolException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } catch (FileNotFoundException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } catch (IOException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 



           }}; 
           t.start(); 
        imgView.setImageUrl(obj.ImageUrl); 
        } 

回答

相關問題