【问题标题】:take photo and upload it to server without any compress android拍照并将其上传到服务器,无需任何压缩 android
【发布时间】:2018-08-25 07:47:04
【问题描述】:

我正在尝试从设备相机拍摄照片或从图库中获取照片并通过 volley 将其上传到服务器 一切正常,但图像质量太差了

private void dispatchTakePictureIntent()
{
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent , CAMERA_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data { 
    switch (requestCode) {
    case CAMERA_REQUEST_CODE:
    if ( resultCode == RESULT_OK){                 
    Bundle bundle = data.getExtras();
    bitmap = (Bitmap) bundle.get("data");
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);                    
}
break;

和 getParams 方法:

    byte[] a = convertBitmapToByteArrayUncompressed(bitmap);
    params.put("img" , Base64.encodeToString(a , Base64.DEFAULT));

public static byte[] convertBitmapToByteArrayUncompressed(Bitmap bitmap){
    ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
    bitmap.copyPixelsToBuffer(byteBuffer);
    byteBuffer.rewind();
    return byteBuffer.array();
}

【问题讨论】:

    标签: php android image upload android-volley


    【解决方案1】:

    从 Naugat 拍照会有所不同。

    创建一个图像文件拳头:

    String mCurrentPhotoPath;
    
    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
        );
    
        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }
    

    然后发送拍照意图

    static final int REQUEST_TAKE_PHOTO = 1;
    
    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                ...
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                                                      "com.example.android.fileprovider",
                                                      photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }
    

    在您的 onActivity 结果中检查 RESULT_OK 是否成功捕获。

    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK)
    

    您已经获得了图像路径。现在使用mCurrentPhotoPath进行上传。

    另外,你需要实现文件提供者。

    在您的清单中添加:

    <application>
       ...
       <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.android.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>
        ...
    </application>
    

    在资源目录中的 XML 中添加:

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />
    </paths>
    

    现在您将获得来自相机的全尺寸图像。

    来源:https://developer.android.com/training/camera/photobasics.html

    【讨论】:

      【解决方案2】:

      您应该必须使用多部分实体来发送未经压缩的图像。使用多部分实体,您的图像质量也将保持不变。请按照此使用凌空发送图像

      public class MultipartReq extends JsonObjectRequest {
      
          private static final String FILE_PART_NAME = "file";
          private static final String STRING_PART_NAME = "text";
      
          private final File mFilePart;
          //private final String mStringPart;
      
      
          MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
          HttpEntity httpEntity;
          Context context;
      
          private Map<String, String> params;
          public MultipartReq(Context context, int method, String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener, File file, Map<String, String> params) {
              super(method, url, jsonRequest, listener, errorListener);
      
              this.context = context;
              mFilePart = file;
              entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);    
              this.params = params;
              buildMultipartEntity();
              httpEntity = entityBuilder.build();
      
          }
      
      
          private void buildMultipartEntity() {
      
              try {
                  if (mFilePart.exists()) {                                           entityBuilder.addBinaryBody(FILE_PART_NAME, mFilePart, ContentType.create(mimeType), mFilePart.getName());
      
                      }
                      try {
                          if(!params.isEmpty()){
                              for (String key: params.keySet()){
                                   entityBuilder.addPart(key, new StringBody(params.get(key),ContentType.TEXT_PLAIN));
      
                              }
                          }
                      } catch (Exception e) {
                          VolleyLog.e("UnsupportedEncodingException");
                      }
      
      
                  } else {
                      ShowLog.e("no such file");
                  }
              } catch (Exception e) {
                  ShowLog.e("UnsupportedEncodingException");
              }
          }
      
          @Override
          public Map<String, String> getHeaders() throws AuthFailureError {
              HashMap<String, String> params = new HashMap<String, String>();
      
              return params;
          }
      
      
          @Override
          public String getBodyContentType() {
              return httpEntity.getContentType().getValue();
          }
      
          @Override
          public byte[] getBody() {
              ByteArrayOutputStream bos = new ByteArrayOutputStream();
              try {
                  httpEntity.writeTo(bos);
              } catch (IOException e) {
                  VolleyLog.e("IOException writing to ByteArrayOutputStream");
              }
              return bos.toByteArray();
          }
      
      
          @Override
          protected void deliverResponse(JSONObject response) {
              super.deliverResponse(response);
          }
      }
      

      【讨论】:

        【解决方案3】:

        使用getParcelableExtra(),而不是getExtras() 用于小尺寸图片。

        Bitmap bitmap = (Bitmap) intent.getParcelableExtra("data");
        

        如果您的图像太大,您必须压缩它们并发送到其他活动。然后你可以得到压缩的位图并在第二个活动中解压缩它。试试下面的代码。

        第一次活动

        Intent intent = new Intent(this, SecondActivity.class);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream);
        byte[] bytes = stream.toByteArray(); 
        intent.putExtra("bitmap",bytes);
        

        第二次活动

        byte[] bytes = getIntent().getByteArrayExtra("bitmap");
        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        

        【讨论】:

          猜你喜欢
          • 2013-10-31
          • 1970-01-01
          • 1970-01-01
          • 2018-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-13
          相关资源
          最近更新 更多