应用程序崩溃使用yalantis uCrop库

时间:2016-03-06 13:25:54

标签: android crop

Yalantis uCrop导入Project后,当我想裁剪图像和调用库方法时,LogCat会显示NullPointerException 裁剪方法是:

    private void doCrop(){
    LTH.dLog(PHOTO_TAG, "Do Crop -> PICK FROM FILE : " + getRealPathFromURI(mImageCaptureUri));
    UCrop.of(mImageCaptureUri, mImageCaptureUri/*getUriFromPath()*/)
            .withAspectRatio(16, 9)
            .withMaxResultSize(UI.width, UI.height)
            .start(PurchaseActivity.this);
}
private String getRealPathFromURI(Uri contentURI) {
    String path;
    Cursor cursor = getContentResolver()
            .query(contentURI, null, null, null, null);
    if (cursor == null)
        path=contentURI.getPath();

    else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        path=cursor.getString(idx);

    }
    if(cursor!=null)
        cursor.close();
    return path;
}
private Uri getUriFromPath(){
    String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
    String DIR_APP = DIR_SDCARD + "/Android/data/" + App.getContext().getPackageName() + "/"+App.getString(App.getContext(), R.string.dir_app)+"/";
    String DIR_CAPTURE = DIR_APP + App.getString(App.getContext(), R.string.dir_capture);
    new File(DIR_APP).mkdirs();
    new File(DIR_APP+DIR_CAPTURE).mkdirs();
    final String IMG_DIR = DIR_APP+DIR_CAPTURE+"/";
    Uri pathUri = Uri.fromFile(new File(IMG_DIR));
    LTH.dLog(PHOTO_TAG, "get Uri From Path -> Uri : "+getRealPathFromURI(pathUri)+" \t "+pathUri);
    return pathUri;
}

错误日志是:

03-06 16:46:20.784  21117-21117/com.emaarit.app.erbgroup E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.emaarit.app.erbgroup, PID: 21117
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emaarit.app.erbgroup/com.yalantis.ucrop.UCropActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5333)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
        at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
        at com.yalantis.ucrop.UCropActivity.setupViews(UCropActivity.java:235)
        at com.yalantis.ucrop.UCropActivity.onCreate(UCropActivity.java:99)
        at android.app.Activity.performCreate(Activity.java:5343)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)

所以我不知道如何解决它

1 个答案:

答案 0 :(得分:1)

使用UCrop.Options

UCrop.Options options = new UCrop.Options();
UCrop.of(uri, mImageCaptureUri)
    .withAspectRatio(10, 10)
    .withMaxResultSize(Width, Height)
    .withOptions(options)
    .start(this);