使用 yalantis uCrop 库的应用程序崩溃
Posted
技术标签:
【中文标题】使用 yalantis uCrop 库的应用程序崩溃【英文标题】:App Crash using yalantis uCrop Library 【发布时间】:2016-03-06 13:25:54 【问题描述】:将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 ComponentInfocom.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)
所以我不知道如何解决它
【问题讨论】:
我认为最好在它的 github 页面上创建一个问题 【参考方案1】:使用UCrop.Options
:
UCrop.Options options = new UCrop.Options();
UCrop.of(uri, mImageCaptureUri)
.withAspectRatio(10, 10)
.withMaxResultSize(Width, Height)
.withOptions(options)
.start(this);
【讨论】:
以上是关于使用 yalantis uCrop 库的应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
(译)uCrop介绍 —— 我们自己的Android图片裁剪库
使用 UICollectionView Yalantis/Koloda 时初始卡片相同