【问题标题】:How to get image from camera or gallery and upload to server in Android Q?如何从相机或图库中获取图像并上传到 Android Q 中的服务器?
【发布时间】:2020-10-09 20:19:30
【问题描述】:

我正在尝试从相机或图库中获取图像以上传到服务器。我的代码在 Android 9 或更低版本中运行良好,但我无法访问 Android 10 中的图像路径。我对 Android 10 的 Scoped Storage 了解不多,请查看我的代码并提供帮助。

 private void selectImage(Context context, final int cameraRequestCode, final int galleryRequestCode) {

    if (!hasPermissions(context, PERMISSIONS)) {
        ActivityCompat.requestPermissions(requireActivity(), PERMISSIONS, PERMISSION_ALL);
    } else {
        final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Cancel"};

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("Choose your profile picture");

        builder.setItems(options, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int item) {

                if (options[item].equals("Take Photo")) {
                   /* Intent takePicture = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(takePicture, cameraRequestCode);*/
                    Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);

                    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

                    startActivityForResult(intent, cameraRequestCode);

                } else if (options[item].equals("Choose from Gallery")) {
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(pickPhoto, galleryRequestCode);

                } else if (options[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }
}

public File convertBitmaptoFile(Bitmap bitmap, String filename) throws IOException {
    File f = new File(requireContext().getCacheDir(), filename);
    f.createNewFile();


    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();


    FileOutputStream fos = new FileOutputStream(f);
    fos.write(bitmapdata);
    fos.flush();
    fos.close();
    return f;

}

'这是我的 onActivity 代码'

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_CANCELED) {
        switch (requestCode) {
            case 0:

                try {
                    String millisecond = String.valueOf(Calendar.getInstance().getTimeInMillis());
                   // logo_file = new File(String.valueOf(convertBitmaptoFile(fileToBitmap(sdImageMainDirectory.getPath()), "IMAGE_" + millisecond + ".jpg")));

                   // img_logo.setImageURI(Uri.parse(logo_file.getAbsolutePath()));
                    img_logo.setImageURI(Uri.parse(getPath(Uri.fromFile(logo_file = new File(String.valueOf(convertBitmaptoFile(fileToBitmap(sdImageMainDirectory.getPath()), "IMAGE_" + millisecond + ".jpg")))))));
                    Log.e("logo file path","" +  logo_file.getPath());
                    Log.e("logo file absolute path","" +  logo_file.getAbsolutePath());
                } catch (IOException e) {
                    e.printStackTrace();
                }


                // fa_image.setImageURI(Uri.parse(fa_image_file.getPath()));

                break;
            case 1:
                if (resultCode == RESULT_OK && data != null) {
                    Uri selectedImage = Uri.parse(data.getData().getEncodedPath());
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    if (selectedImage != null) {
                        Cursor cursor = getActivity().getContentResolver().query(selectedImage,
                                filePathColumn, null, null, null);
                        if (cursor != null) {
                            cursor.moveToFirst();

                            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                            String picturePath = cursor.getString(columnIndex);
                            logo_file = new File(picturePath);
                            Log.e("IMAGE", "ja_image :" + logo_file);
                            img_logo.setImageBitmap(BitmapFactory.decodeFile(picturePath));
                            cursor.close();
                        }
                    }

                }
                break;

        }
    }
}

【问题讨论】:

    标签: java android android-studio android-fragments android-10.0


    【解决方案1】:

    你在AndroidManifest.xml中使用过android:requestLegacyExternalStorage="true"

      <application
        android:name="com.xyz"
        android:allowBackup="true"
        android:exported="false"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:requestLegacyExternalStorage="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true"
        tools:ignore="GoogleAppIndexingWarning"
        tools:targetApi="q">
    

    【讨论】:

    • 是的,我已经使用了 'android:requestLegacyExternalStorage" 但仍然面临同样的问题
    【解决方案2】:

    在 Android 10 中,您无法使用 onActivityResult 中提供的 uri 访问图像。尝试这个。我相信它会为你工作。当您从图库中选择图像时,这将起作用。从相机照片中拍照,方法有点不同。

    Kotlin 解决方案:

    val imageType = contentResolver.getType(data.data!!)
    
    data.data!!.let {
                    application.contentResolver.openInputStream(it).use { inputStream ->
                        filePartImage = MultipartBody.Part.createFormData(
                            "image",     //should be same as the key of your parameter
                            "filename" + ".jpg",  // extension of file name is must
                            inputStream!!.readBytes().toRequestBody(imageType.toMediaTypeOrNull())
                        )
                    }
                }
    

    稍后将此 filePartImage 作为您的图像参数传递给服务器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-25
      • 2021-04-30
      • 2016-09-16
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      相关资源
      最近更新 更多