android 拍照上传到服务器,如何在androidq中从相机或图库获取图像并上传到服务器?...

我正在尝试从相机或图库中获取图像以上载到服务器上。我的代码在android9或更低版本中运行得很好,但我无法访问android10中的图像路径。我对android10s范围存储不太了解,请查看我的代码和帮助。

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;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值