【问题标题】:Can't upload image and video from gallery in some Android mobiles无法在某些 Android 手机中从图库中上传图像和视频
【发布时间】:2017-11-14 16:50:39
【问题描述】:

我是安卓开发新手。我正在处理画廊中的图像和视频。我从图库上传图片或视频。我得到游标值返回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();
            }
        }`

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我在清单文件中提到了权限。我将目标 sdk 设置为 22。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    相关资源
    最近更新 更多