In my app, I have a video upload option which allows user to upload the video. everything works fine except when user tried to pick a video from external storage. the app will just crash and give NullPointerException
. Can someone please explain why? how come the function works fine when user choose video from gallery or phone storage but not external storage? this is the error message I got
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=file:///mnt/sdcard/Android/data/com.dropbox.android/files/scratch/VIDEO0010.mp4 dat=file:///mnt/sdcard/Android/data/com.dropbox.android/files/scratch/VIDEO0010.mp4 }} to activity {com.myactivity.android/com.myactivity.android.Webviewer}: java.lang.NullPointerException
and below is my code
private VideoFileInfo getVideoInfo(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION, MediaStore.Video.Media.TITLE };
Cursor cursor = managedQuery(uri, projection, null, null, null);
cursor.moveToFirst(); //this is where the app crashed
String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
String fileName = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
int fileSize = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
long duration = TimeUnit.MILLISECONDS.toSeconds(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)));
System.out.println("path: " + filePath);
System.out.println("name: " + fileName);
System.out.println("size: " + fileSize);
System.out.println("duration: " + duration);
vfi.setPathNameSizeDuration(filePath, fileName, fileSize, duration);
return vfi;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case FILECHOOSER_REQUESTCODE_VIDEO:
if (resultCode == Activity.RESULT_OK) {
System.out.println("SELECT_AUDIO");
Uri selectedVideoUri = intent.getData();
selectedPath = getVideoInfo(selectedVideoUri).getPath();
System.out.println("SELECT_AUDIO Path : " + selectedPath);
//bm = BitmapFactory.decodeFile(selectedPath);
blVideoDail = true;
}
break;
}
}
private void createVideoIntent(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("video/*");
Intent chooser = createVideoChooserIntent(createVideoFromCameraIntent());
chooser.putExtra(Intent.EXTRA_INTENT, intent);
startActivityForResult(chooser, FILECHOOSER_REQUESTCODE_VIDEO);
}
private Intent createVideoChooserIntent(Intent... intents) {
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);
chooser.putExtra(Intent.EXTRA_TITLE, "Please Choose Your Video");
return chooser;
}
private Intent createVideoFromCameraIntent() {
return new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
}
can someone help please? Thanks
the app crashed when hit cursor.moveToFirst();
.
FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=file:///mnt/sdcard/Android/data/com.dropbox.android/files/scratch/VIDEO0010.mp4 dat=file:///mnt/sdcard/Android/data/com.dropbox.android/files/scratch/VIDEO0010.mp4 }} to activity {com. myactivity .android/com.myactivity.android.Webviewer}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3387)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3437)
at android.app.ActivityThread.access$1100(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1291)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.myactivity.android.Webviewer.getVideoInfo(Webviewer.java:455)
at com.myactivity.android.Webviewer.onActivityResult(Webviewer.java:500)
at android.app.Activity.dispatchActivityResult(Activity.java:4740)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3383)
... 11 more
Use the following code to select Video from the phone / card
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
Above code will give you file name of the selected item