2

내 앱에는 사용자가 동영상을 업로드 할 수있는 동영상 업로드 옵션이 있습니다. 사용자가 외부 저장소에서 동영상을 선택하려고 할 때를 제외하고는 모든 것이 잘 작동합니다. 응용 프로그램이 그냥 충돌하고 NullPointerException 줄 것이다. 누군가 이유를 설명해 주시겠습니까? 사용자가 갤러리 나 휴대 전화 저장소에서 동영상을 선택하지만 외부 저장소에서 동영상을 선택하면 기능이 제대로 작동합니까? 이것은 내가 아래외부 저장소 (sdcard)에서 비디오에 액세스하려고하면 android-get nullpointerexception이 발생합니다.

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 

와 내 코드

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); 
} 

할 수있는 누군가가 도와 줘요있어 오류 메시지인가? 감사합니다

때 응용 프로그램이 충돌했을 때 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 
+0

logcat. null 포인터 예외가 발생하는 행을 알려주십시오. 더 나은 여전히, 그 라인을 직접 봐 - 그것은 일반적으로 알아 내기가 일반적으로 쉽습니다 ... – IanB

+0

실제 장치 에서이 코드를 테스트하고 있습니까? – Mohit

+0

예. 실제 장치에 – user1865027

답변

-1

공개 무효 selectFile() {의도 의도 = 새로운 텐트(); intent.setType ("video/*"); intent.setAction (Intent.ACTION_GET_CONTENT); startActivityForResult (Intent.createChooser (의도, "비디오 선택 "), SELECT_VIDEO);

}

사용하여 전화/카드

@Override에서 비디오를 선택하려면 다음 코드 공공 무효 onActivityResult를 (INT requestCode가, INT의 resultCode, 의도 데이터) {

if (resultCode == RESULT_OK) { 
     if (resultCode == Activity.RESULT_OK) { 
      if (requestCode == SELECT_VIDEO) 
       onSelectFromGalleryResult(data); 
      /* 
      * else if (requestCode == REQUEST_CAMERA) 
      * onCaptureImageResult(data); 
      */} 
    } 
} 

@SuppressWarnings("deprecation") 
private void onSelectFromGalleryResult(Intent data) { 
    Uri selectedImageUri = data.getData(); 
    String[] projection = { MediaColumns.DATA }; 
    Cursor cursor = managedQuery(selectedImageUri, projection, null, null, 
      null); 
    int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); 
    cursor.moveToFirst(); 

    String selectedImagePath = cursor.getString(column_index); 

    File fileUpload = new File(selectedImagePath); 
    // selected_image_file.add(fileUpload); 
    // new UploadAdvertisement().execute(); 

    Bitmap bm; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(selectedImagePath, options); 
    final int REQUIRED_SIZE = 200; 
    int scale = 1; 
    while (options.outWidth/scale/2 >= REQUIRED_SIZE 
      && options.outHeight/scale/2 >= REQUIRED_SIZE) 
     scale *= 2; 
    options.inSampleSize = scale; 
    options.inJustDecodeBounds = false; 
    bm = BitmapFactory.decodeFile(selectedImagePath, options); 
    ImageView image = new ImageView(getApplicationContext()); 
    // image.setId(adverticse_image_start_id); 
    int width = (int) (getResources().getDimension(R.dimen.dim_100)/getResources() 
      .getDisplayMetrics().density); 
    int left_margin = (int) (getResources().getDimension(R.dimen.dim_15)/getResources() 
      .getDisplayMetrics().density); 
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(width, 
      LayoutParams.MATCH_PARENT); 
    param.setMargins(left_margin, 0, 0, 0); 

    image.setLayoutParams(param); 
    image.setImageBitmap(bm); 
    System.out.println("selected video path >>>>" + selectedImagePath); 
    if (selectedImagePath != null) { 
     new UploadFileToserver().execute(); 
    } 
} 

위 코드는 선택한 항목의 파일 이름을 제공합니다.

+0

개인 정적 최종 int SELECT_VIDEO = 3, 전역 변수로 사용 –