在某些Android手机中无法从图库上传图片和视频

时间:2017-06-13 04:40:37

标签: android android-6.0-marshmallow android-gallery android-cursor

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

第一

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

2)如何解决这个错误?

我曾经从图库

上传图片和视频
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();
            }
        }`

1 个答案:

答案 0 :(得分:0)

由于您已经提到过Android 6.0,因此请在尝试之前检查您是否具有读取和写入磁盘存储的权限。从Marshmallow开始,Android已启动运行时权限,这意味着默认情况下,除非您在运行时提供,否则Android Manifest中提供的大多数权限实际上都不起作用

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