在安卓Nougat上如何裁剪图片

3

我已将手机更新至安卓nougat版本,但是剪裁功能突然不起作用了。在Marshmallow版本中,该功能运行得非常完美。

这是我的build.gradle文件:

    apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
    applicationId "multas.com.usj.multaszgz"
    minSdkVersion 23
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile project(':tess-two-release')
compile files('libs/androidjhlabs.jar')
//Pestañas
compile files('libs/commons-codec-1.7.jar')
compile project(path: ':openCVLibrary310')
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha4'
compile 'com.android.support:design:24.2.0'
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.itextpdf:itextg:5.5.9'
compile 'org.codepond:wizardroid:1.3.1'
compile 'com.google.android.gms:play-services:9.4.0'
testCompile 'junit:junit:4.12'
}

以下是裁剪的代码:

  private void crop(){
try {

  Log.v("OCRActivity", "HA LLEGADO AL CROP: "+takeStoragePermissionGranted());
  File file = new File(path);
  Uri outputFileUri = Uri.fromFile(file);

  Intent intent = new Intent("com.android.camera.action.CROP");
  intent.setDataAndType(outputFileUri, "image/*");
  intent.putExtra("crop", true);
  //intent.putExtra("outputX",bm.getWidth());
  //intent.putExtra("outputY", bm.getHeight());
  intent.putExtra("aspectX", 0);
  intent.putExtra("aspectY", 0);
  intent.putExtra("scale", true);
  intent.putExtra("return-data", true);
  Log.v("OCRActivity", "HA LLEGADO ");

  startActivityForResult(intent, PIC_CROP);

}
catch(ActivityNotFoundException anfe){
  //display an error message
  String errorMessage = "Whoops - your device doesn't support the crop action!";
  Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
  toast.show();
}catch(Exception e){
  Log.e("OCRActivity", "crop():"+e.getMessage());
}

   }

我在控制台中没有检测到任何错误,但在onActivityOnResult中返回的结果代码为0,而应该是1...

我不知道nougat发生了什么事情,使用marshmallow时相同的代码可以正常工作...

请问有什么解决方法吗?


Android没有CROP Intent。有许多可用于Android的图像裁剪库,请使用其中之一。 - CommonsWare
好的,谢谢!那么,我的问题是...我能在Android Nougat中包含AOSP相机吗?我不想导入一个裁剪库,因为这个应用程序是为我自己而不是任何手机或用户而设计的。 - Antonio Herrero Martínez
最后我安装了这个相机的.apk(http://www.qbking77.com/apps-games/download-the-camera-and-live-wallpaper-from-google-play-edition-devices/),它完美地运行了!感谢CommonsWare提供的关于裁剪意图的信息。在我看到的所有网站上,似乎camera.action.crop是一个Android指令... - Antonio Herrero Martínez
请问您能提供您的解决方案吗?我也遇到了同样的问题。 - Shweta Chauhan
Crop意图使用在搭载AOSP的手机上安装的一些应用程序。Nougat中的原始相机不使用AOSP,因此解决问题的最佳方法是安装像 http://www.qbking77.com/apps-games/download-the-camera-and-live-wallpaper-from-google-play-edition-devices/ 这样的兼容应用程序。这是我唯一的解决办法... - Antonio Herrero Martínez
2个回答

0

来自Android -N,传递文件:// URI到包域之外可能会导致接收方无法访问路径。因此,尝试传递文件:// URI会触发FileUriExposedException异常。

有关详细信息,请查看文章

还要注意一点,如果您从图库选择图像的API >= 24,则必须将输出URI添加到裁剪意图中,例如:

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(YOUR_FILE_PATH));


0

我在我的安卓项目中使用了这个函数。 希望它能对你有所帮助。

// 用于裁剪图像的函数

 private void croppingIMG() {
    imageSelect = true;
    final ArrayList cropOptions = new ArrayList();
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");

    List list = getActivity().getPackageManager().queryIntentActivities(intent, 0);
    int size = list.size();
    if (size == 0) {
        Toast.makeText(getActivity(), "Cann't find image croping app", Toast.LENGTH_SHORT).show();
        return;
    } else {
        intent.setData(mImageCaptureUri);
        intent.putExtra("outputX", 512);
        intent.putExtra("outputY", 512);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        //TODO: don't use return-data tag because it's not return large image data and crash not given any message
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outPutFile));

        if (size == 1) {
            Intent i   = new Intent(intent);
            ResolveInfo res = (ResolveInfo) list.get(0);
            i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            startActivityForResult(i, CROPING_CODE);
        } else {

            Intent i   = new Intent(intent);
            ResolveInfo res = (ResolveInfo) list.get(0);
            i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            startActivityForResult(i, CROPING_CODE);
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接