简体   繁体   English

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

[英]Can't upload image and video from gallery in some Android mobiles

I am new to android development. 我是android开发的新手。 I am working on images and videos in gallery. 我正在处理画廊中的图像和视频。 I upload the image or video from gallery. 我从图库上传图像或视频。 I got the cursor values return null. 我得到的游标值返回null。

First 第一

1) Why the issue is occurred when upload image or video ? 1)为什么上传图片或视频时会发生此问题?

2) How to solve this error? 2)如何解决这个错误?

I used to upload and image and video from 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` 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();
            }
        }`

Since you have mentioned Android 6.0, just check if you have the permissions given to read and write the disk storage before you try it. 既然已经提到了Android 6.0,请在尝试之前检查一下您是否具有读写磁盘存储的权限。 Starting from Marshmallow, Android has started runtime permissions which means by default most of the permissions provided in the Android Manifest does not actually work unless you provide it at runtime 从棉花糖开始,Android已启动运行时权限,这意味着默认情况下,除非您在运行时提供,Android Manifest中提供的大多数权限实际上不会起作用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM