当前位置: 首页 > 知识库问答 >
问题:

从Google Photos上传照片,而不是从android的gallery和file manager上传照片

郭德惠
2023-03-14

我正在尝试从sdcard拍摄照片,它是工作的情况下,当用户选择从谷歌照片,但给出一个curserindexoutofbound错误时,从任何其他应用程序,如gallery或file Manager。这是一段代码。

OnActivity Result`{if(requestCode==PICK_IMAGE_REQUEST&&resultCode==activity.result_ok&data!=null&data.getdata()!=null){Uri Uri=null;String realPath=null;

        try
        {
            uri = data.getData();
            Log.e("uri",uri.toString());
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);

            imageView.setImageBitmap(bitmap);

            String imagePath = getRealPathFromURI(uri);

            File source = new File(imagePath);

            String destinationPath = Environment.getExternalStorageDirectory().toString() + "/IASFolders/"+Configuration.empcode+".jpg";



            File destination = new File(destinationPath);
            try
            {
                InputStream in = new FileInputStream(source.getAbsolutePath());
                OutputStream out = new FileOutputStream(destination.getAbsolutePath());

                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0)
                {
                    out.write(buf, 0, len);
                }

                in.close();
                out.close();

            }
            catch (IOException e)
            {
                e.printStackTrace();
                Log.d("File Copy Exception", e.toString());
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
private String getRealPathFromURI(Uri contentURI) {
    String result;
    Cursor cursor = getActivity().getContentResolver()
            .query(contentURI,new String[]{MediaStore.Images.Media.DATA},MediaStore.Images.Media.DISPLAY_NAME+"=?" ,new String[]{Configuration.empcode},null);

    /*
    * Cursor imageCursor=getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,new String[]{MediaStore.Images.Media.DATA},MediaStore.Images.Media.DISPLAY_NAME+"=?" ,new String[]{imageTitle},null);
                    imageCursor.moveToFirst();
                    String imageData=imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                    Long imageSize=imageCursor.getLong(imageCursor.getColumnIndex(ImageColumns.SIZE));
                    Toast.makeText(getApplicationContext(), String.valueOf(imageSize), Toast.LENGTH_LONG).show();*/
    if (cursor == null)
    {
        result = contentURI.getPath();
    }
    else
    {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
        result = cursor.getString(idx);
        cursor.close();
    }
    return result;
}

}`

共有1个答案

澹台新知
2023-03-14

像这样试试

//while setting Intent
   if (Build.VERSION.SDK_INT <= 19) {
                                            Intent intent = new Intent();
                                            intent.setType("image/*");
                                            intent.setAction(Intent.ACTION_GET_CONTENT);
                                            intent.addCategory(Intent.CATEGORY_OPENABLE);
                                            startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
                                        } else if (Build.VERSION.SDK_INT > 19) {
                                            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                                            startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);


                }


                // get result after selecting image from Gallery
                @Override
                public void onActivityResult(int requestCode, int resultCode, Intent data) {
                    super.onActivityResult(requestCode, resultCode, data);

                    if (requestCode == PICK_IMAGE && resultCode == getActivity().RESULT_OK && null != data) {
                        Uri selectedImageUri = data.getData();
                        String selectedImagePath = getRealPathFromURIForGallery(selectedImageUri);
                        decodeFile(selectedImagePath);
                    }
                }

                public String getRealPathFromURIForGallery(Uri uri) {
                    if (uri == null) {
                        return null;
                    }
                    String[] projection = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
                    if (cursor != null) {
                        int column_index = cursor
                                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                        cursor.moveToFirst();
                        return cursor.getString(column_index);
                    }
                    return uri.getPath();
                }     
                  // decode image
                public void decodeFile(String filePath) {
                    // Decode image size
                    BitmapFactory.Options o = new BitmapFactory.Options();
                    o.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(filePath, o);
                    // The new size we want to scale to
                    final int REQUIRED_SIZE = 1024;
                    // Find the correct scale value. It should be the power of 2.
                    int width_tmp = o.outWidth, height_tmp = o.outHeight;
                    int scale = 1;
                    while (true) {
                        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
                            break;
                        width_tmp /= 2;
                        height_tmp /= 2;
                        scale *= 2;
                    }

                    // Decode with inSampleSize
                    BitmapFactory.Options o2 = new BitmapFactory.Options();
                    o2.inSampleSize = scale;
                    bitmap = BitmapFactory.decodeFile(filePath, o2);
                    Security connection = new Security(context);
                    Boolean isInternetPresent = connection.isConnectingToInternet(); // true or false
                    if (isInternetPresent) {
                        // submit usr information to server
                        //first upload file
                        updateUserProfileImage();
                        Log.i("IMAGEPATH", "" + imagePath);
                    }
                    profileImageView.setImageBitmap(bitmap);
                }
 类似资料:
  • 我收到代码为200的响应,但图像没有上传到服务器

  • 我是android studio和手机应用程序开发的初学者,但我仍在努力学习。因此,我设法构建了我的第一个webview应用程序来显示页面和图片。我发现了许多有用的资源来帮助我这样做,但我仍然面临着一个问题,即如何直接上传从手机摄像头(而不是从图库)拍摄的照片并上传到服务器。 1-在我按下眉毛按钮后,应用程序会提示我是从相机还是从画廊拍摄照片。(Android版9)2-当我从图库上传一张照片时,应

  • 我正在为Android开发一个包装应用程序,需要连接到相机/照片库(这样用户就可以上传一张自己的照片,例如)。我发现了许多关于这个主题的帖子,包括在WebView中从相机或图库上传图像,以及从WebView输入字段上传相机照片和文件回声器,但我根据这些解决方案建模的代码不起作用。我在openFileChooser函数中放置了一个print语句,当在WebView中单击add Photo/Docum

  • 操作步骤: ①在"图层管理"模块,选择一个带有数据的图层。 ②点击记录弹出窗口。 ③点击"上传图片"按钮。 ④可以添加标注地点的照片信息,支持从本地上传和选择网络上的图片以及历史图片。 ⑤选择图片,进行添加图片。 ⑥点击"绑定图片"按钮。 提示: ●目前一个网点支持上传20张照片。 ●每张图片大小不超过5M。 操作动图: [查看原图]

  • 我已经处理了文件选择器,它完美地上传了文件, <罢工> 问题出在相机上。一旦我试图上传相机照片,它崩溃了。据我所知,这是因为URI。 a)文件选择器:内容://media/external/images/1234 b)摄像:文件:///mnt/sdcard/pic.jpg <罢工> 它现在崩溃了,因为在尝试上传“content://media/external/images/1234”时出现了nu

  • 我试图构建一个,它包括两个按钮,一个用于用相机拍照并上传到Firebase存储,另一个用于从Firebase存储下载图像并在上显示。 现在,我被上传功能卡住了。我可以把照片保存到应用程序目录中。我想上传保存的图片到Firebase存储。