gpt4 book ai didi

android - 使用 yalantis uCrop 库的应用程序崩溃

转载 作者:太空狗 更新时间:2023-10-29 14:00:40 26 4
gpt4 key购买 nike

导入后 Yalantis uCrop到项目,当我想裁剪图像和调用库方法时,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)

所以我不知道如何修复它

最佳答案

使用UCrop.Options:

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

关于android - 使用 yalantis uCrop 库的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35827399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com