2014-09-11 90 views
0

我想上傳圖片大小相同選擇從Gallary.i.Uploaded到服務器..But圖像上傳到服務器上像壓縮....像這張圖片下方 enter image description here如何上傳圖片到服務器在android

我想在相同大小...或如何設置裁剪意圖在這了Methode上傳圖片

這裏是我的代碼來調整圖像

public void onActivityResult(int requestCode, int resultCode, Intent data) { 

     switch (requestCode) { 
     case 123987: 
      if (resultCode == RESULT_OK) { 
       try { 

        Uri selectedImageUri = data.getData(); 
        selectedImagePath = getPath(selectedImageUri); 
        Log.d("selectedImagePath", "selectedImagePath = " + selectedImagePath); 

        ParseImage _ParseImage = new ParseImage(); 
        _ParseImage.activityContext = this; 
        _ParseImage.execute(selectedImagePath); 

       } catch (Exception e) { 
        // TODO: handle exception 
       } 
      } 

      break; 

     case 1234: 
      if (resultCode == RESULT_OK) { 
       try { 

        final_bitmap = null; 

        String realPath = Environment.getExternalStorageDirectory() + "/" + fileName; 

        selectedImagePath = realPath; 

        new BitmapFactory(); 

        Bitmap btmp = setBitmap(selectedImagePath); 

        /*String path = selectedImagePath.toLowerCase(); 
        if (path.contains("dcim") && path.contains("camera")) { 
         btmp = RotateBitmap(btmp, 90); 
        }*/ 

        int o_width = btmp.getWidth(); 
        int o_height = btmp.getHeight(); 

        // w=1840, h=3264 
        float large_image_ratio = 300.00f; 

        //Log.d("orignal_image", "orignal_image = " + o_width + " , " + o_height); 
        if (o_width > o_height) { 

         if (o_width >= large_image_ratio) { 

          float temp_w = (float)o_width/(float)large_image_ratio; 
          //int temp_h = o_height/large_image_ratio; 

          //Log.d("temp_w", "temp_w = " + temp_w); 

          int scales_width = (int) large_image_ratio; 
          int scales_height = (int) Math.round(o_height/temp_w); 

          //Log.d("scale_image-if", "scale_image-if = " + scales_width + " , " + scales_height); 

          final_bitmap = Bitmap.createScaledBitmap(btmp, scales_width, scales_height, true); 

         } else { 
          final_bitmap = btmp; 
         } 
        } else { 


         if (o_height >= large_image_ratio) { 

          //int temp_w = o_width/large_image_ratio; 
          float temp_h = (float)o_height/(float)large_image_ratio; 

          //Log.d("temp_h", "temp_h = " + temp_h); 

          int scales_width = (int) Math.round(o_width/temp_h); 
          int scales_height = (int) large_image_ratio; 

          //Log.d("scale_image-else", "scale_image-else = " + scales_width + " , " + scales_height); 

          final_bitmap = Bitmap.createScaledBitmap(btmp, scales_width, scales_height, true); 
         } else { 
          final_bitmap = btmp; 
         } 

        } 

        img.setImageBitmap(final_bitmap); 

        Image_into_Byte(final_bitmap); 
        img.setVisibility(View.VISIBLE); 
        btn_upload.setEnabled(true); 

       } catch (Exception e) { 
        // TODO: handle exception 
       } 
      } 
      break; 
     } 
    } 

    public class ParseImage extends AsyncTask<String, Void, String> { 

     private ProgressDialog dialog; 
     protected Context activityContext; 


     @Override 
     protected void onPreExecute() { 
      try { 
       final_bitmap = null; 

       this.dialog = new ProgressDialog(activityContext, AlertDialog.THEME_HOLO_LIGHT); 
       //this.dialog.setTitle(title); 
       this.dialog.setMessage("Loading Image"); 
       this.dialog.setCanceledOnTouchOutside(false); 
       this.dialog.show(); 


      } catch (Exception e) { 
       // TODO: handle exception 
      } 
     } 

     protected String doInBackground(String... image_path) { 
      try { 

       new BitmapFactory(); 

       Bitmap btmp = setBitmap(selectedImagePath); 

       /*String path = selectedImagePath.toLowerCase(); 
       if (path.contains("dcim") && path.contains("camera")) { 
        btmp = RotateBitmap(btmp, 90); 
       }*/ 

       int o_width = btmp.getWidth(); 
       int o_height = btmp.getHeight(); 

       // w=1840, h=3264 
       float large_image_ratio = 300.00f; 

       //Log.d("orignal_image", "orignal_image = " + o_width + " , " + o_height); 
       if (o_width > o_height) { 

        if (o_width >= large_image_ratio) { 

         float temp_w = (float)o_width/(float)large_image_ratio; 
         //int temp_h = o_height/large_image_ratio; 

         //Log.d("temp_w", "temp_w = " + temp_w); 

         int scales_width = (int) large_image_ratio; 
         int scales_height = (int) Math.round(o_height/temp_w); 

         //Log.d("scale_image-if", "scale_image-if = " + scales_width + " , " + scales_height); 

         final_bitmap = Bitmap.createScaledBitmap(btmp, scales_width, scales_height, true); 

        } else { 
         final_bitmap = btmp; 
        } 
       } else { 


        if (o_height >= large_image_ratio) { 

         //float temp_w = o_width/large_image_ratio; 
         float temp_h = (float)o_height/(float)large_image_ratio; 

         //Log.d("temp_h", "temp_h = " + temp_h); 

         int scales_width = (int) Math.round(o_width/temp_h); 
         int scales_height = (int) Math.round(o_width/temp_h); 

         //Log.d("scale_image-else", "scale_image-else = " + scales_width + " , " + scales_height); 

         final_bitmap = Bitmap.createScaledBitmap(btmp, scales_width, scales_height, true); 
        } else { 
         final_bitmap = btmp; 
        } 

       } 

      } catch (Exception e) { 
       // TODO: handle exception 
      } 
      return "complete"; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      try { 
       this.dialog.cancel(); 
       SaveBitmap(final_bitmap); 

       // final_bitmap 
       img.setImageBitmap(final_bitmap); 

       Image_into_Byte(final_bitmap); 

       btn_upload.setEnabled(true); 

      } catch (Exception e) { 
       // TODO: handle exception 
      } 
     } 
    } 
+0

然後不要弄亂位圖和縮放和調整大小。它只是一個你想要上傳的文件。上傳文件的示例,您可以在此網站上找到許多文件。 – greenapps 2014-09-11 07:00:33

回答

0
// crop intent function 

    public void cropCapturedImage(Uri picUri){ 
     //call the standard crop action intent 
     Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
     //indicate image type and Uri of image 
     cropIntent.setDataAndType(picUri, "image/*"); 
     //set crop properties 
     cropIntent.putExtra("crop", "true"); 
     //indicate aspect of desired crop 
     cropIntent.putExtra("aspectX", 1); 
     cropIntent.putExtra("aspectY", 1); 
     //indicate output X and Y 
     cropIntent.putExtra("outputX", 256); 
     cropIntent.putExtra("outputY", 256); 
     //retrieve data on return 
     cropIntent.putExtra("return-data", true); 
     //start the activity - we handle returning in onActivityResult 
     startActivityForResult(cropIntent, 2); 
    } 


get uri from gallery selected image 

    private String getRealPathFromURI(Uri contentURI) { 
     String result; 
     Cursor cursor = getContentResolver().query(contentURI, null, null, null, null); 
     if (cursor == null) { // Source is Dropbox or other similar local file path 
      result = contentURI.getPath(); 
     } else { 
      cursor.moveToFirst(); 
      int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
      result = cursor.getString(idx); 
      cursor.close(); 
     } 
     return result; 
    } 


    use this function when get image from gallery and camera :- 

    // camera on activityresult 
    cropCapturedImage(Uri.fromFile(file)); 


    // gallery 

    cropCapturedImage(Uri.fromFile(getRealPathFromURI(data.getData()))); 
0

您應該製作Base64編碼的字符串Bitmap並通過NameValuePairurl將此圖像字符串發送到服務器。使用AsyncTask<...>上傳圖像例如像:

private class ImgUploadingTask extends AsyncTask<String, Void, String> 
{ 

    //do your stuff onPreExecute()  

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

      String imageDataString = null; 

      HttpClient client = new DefaultHttpClient(); 

      HttpPost post = new HttpPost(server_url); 

      List<NameValuePair> list = new ArrayList<NameValuePair>(); 

      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

      bitmap.compress(CompressFormat.JPEG, 100, bos); 

      byte[] data = bos.toByteArray(); 

      imageDataString = Base64.encodeToString(data, Base64.DEFAULT); 

      list.add(new BasicNameValuePair("image", imageDataString)); 

      post.setEntity(new UrlEncodedFormEntity(list)); 

      HttpResponse resp = client.execute(post); 

      HttpEntity entity = resp.getEntity(); 

      String response = EntityUtils.toString(entity); 

      Log.d("Response ", response); 
      } 
      catch(Exception e){ 
      e.printStackTrace(); 
      } 
      return null; 

     } 
} 

    //do your stuff onPostExecute() 

這一點,你必須設置你的服務器端代碼後,接受這個Base64 encoded字符串和decode它在服務器端得到實時圖像。

+0

Base64編碼圖像不是推薦的方式,因爲它將傳輸量增加了30%。更好地發送文件'原樣'。 – greenapps 2014-09-11 07:02:15