0

我是android开发新手。我正在处理图库中的图像和视频。我从图库上传图片或视频。我得到游标值返回null。无法从部分Android手机上的图片库中上传图像和视频

首先

1)为什么出现问题时上传图片或视频?

2)如何解决这个错误?

我用来上传和gallery`图像和视频

OpenGallery.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       mode = "sms"; 
       gocodercamera.stopPreview(); 


       isCameraMenuVisible = false; 
       CameraMenu.setVisibility(View.GONE); 
       Intent galleryIntent = new Intent(/*Intent.ACTION_GET_CONTENT*/Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       galleryIntent.setType("image/* video/*"); 
       startActivityForResult(galleryIntent, 300); 
      } 
     }); 
` 

OnActivityResult`

 /**Picking file from Gallery*/ 
     if (requestCode == 300) { 

      if (resultCode == RESULT_OK) { 

       try { 

        gocodercamera.setVisibility(View.VISIBLE); 
        gocodercamera.startPreview(); 
        if (data != null) { 

         Uri selectedImage = data.getData(); 

         String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
         Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
         cursor.moveToFirst(); 
         int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
         String filepath = cursor.getString(columnIndex); 
         cursor.close(); 


         if (filepath.endsWith(".jpg") || filepath.endsWith(".jpe") || filepath.endsWith(".jpeg") || filepath.endsWith(".png")) { 

          imagefilepath = filepath; 
//       uriList.add(imagefilepath); 
//       typesList.add("image"); 

          Uri getCompressedUriPath = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
          String getCompressedPath = compressImage(filepath, getCompressedUriPath); 
          if (aDetector.isConnectingToInternet()) { 
           sendMediaUpload(getCompressedPath, "image"); 
          } else { 
           Toast.makeText(getApplicationContext(), "Please check your internet connection", Toast.LENGTH_LONG).show(); 
          } 


//       ImageAdapterList myAdapter = new ImageAdapterList(); 
//       listview.setAdapter(myAdapter); 

         } else { 

          Cursor cursor1 = MediaStore.Video.query(getContentResolver(), selectedImage, new String[]{MediaStore.Video.VideoColumns.DURATION}); 
          cursor1.moveToFirst(); 
          String dur = cursor1.getString(cursor1.getColumnIndex("duration")); 
          int duration = Integer.parseInt(dur); 

          duration = duration/1000; 

          Log.e("dur", "dur : " + dur); 
          Log.e("duration", "duration : " + duration); 


          videofilepath = filepath; 
          imagefilepath = videofilepath; 
          sendMediaUpload(imagefilepath, "video"); 


//        ImageAdapterList myAdapter = new ImageAdapterList(); 
//        listview.setAdapter(myAdapter); 


         } 
        } else { 
//      Toast.makeText(getApplicationContext(),"Sorry! Failed to select File from gallery", Toast.LENGTH_SHORT).show(); 
        } 

       } catch (NullPointerException e) { 
        e.printStackTrace(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } else if (resultCode == RESULT_CANCELED) { 

       gocodercamera.setVisibility(View.VISIBLE); 
       gocodercamera.startPreview(); 
//    Toast.makeText(getApplicationContext(),"User cancelled to select File", Toast.LENGTH_SHORT).show(); 
      } else { 

       gocodercamera.setVisibility(View.VISIBLE); 
       gocodercamera.startPreview(); 
//    Toast.makeText(getApplicationContext(),"Sorry! Failed to select File", Toast.LENGTH_SHORT).show(); 
      } 
     }` 

回答

0

既然你提到的Android 6.0,只是检查是否有给读写权限磁盘存储在您尝试之前。从棉花糖开始,Android已经开始运行时权限,这意味着在默认情况下大多数Android清单提供的权限,实际上并没有工作,除非你在运行时

https://developer.android.com/training/permissions/requesting.html

+0

我提到在清单文件的权限提供。我设定了目标sdk 22。 –