如何从画廊或相机中获取图像并在Android中高质量裁剪

时间:2017-04-01 12:40:48

标签: android image android-camera-intent

您好我必须为我的应用设置个人资料图片。我已经完成了从画廊和相机拍摄图像,然后将其裁剪为完美的形状并在我的图像视图中显示,但它是低质量像素,图像不清晰。 如何获得相机拍摄的相同质量或图库中的图像

我的代码是:

public String getContactByPhoneNumber(String phoneNumber) throws Exception {
    HttpURLConnection connection = null;
    URL url = new URL(completeURL + "/contacts?select=lastname&$filter=telephone1 eq '"+phoneNumber+"'");
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("OData-MaxVersion", "4.0");
    connection.setRequestProperty("OData-Version", "4.0");
    connection.setRequestProperty("Accept", "application/json");
    connection.setRequestProperty("Prefer", "odata.include-annotations=\"*\"");
    connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
    connection.addRequestProperty("Authorization", "Bearer " + token);
    if (connection.getResponseCode() == 200) {
        String response = getResponseAsString(connection);
        return response;
    } else {
        String error = getErrorAsString(connection);
        System.err.println(error);
        return null;
    }

}

1 个答案:

答案 0 :(得分:0)

  1. 将库包含为本地库项目。将此行添加到build.gradle文件

    compile 'com.yalantis:ucrop:2.2.0'
    
  2. 将UCropActivity添加到AndroidManifest.xml

     <activity
    android:name="com.yalantis.ucrop.UCropActivity"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
    
  3. 使用构建器模式创建uCrop配置。

      UCrop.of(sourceUri, destinationUri)
    .withAspectRatio(16, 9)
      .withMaxResultSize(maxWidth, maxHeight)
     .start(context);
    
  4. 覆盖onActivityResult方法并处理uCrop结果。

     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
        final Uri resultUri = UCrop.getOutput(data);
    } else if (resultCode == UCrop.RESULT_ERROR) {
        final Throwable cropError = UCrop.getError(data);
    }
    }