您可以使用这些函数通过相机捕捉图像:
public static Uri getTakenPictureUri(Context mContext, Intent data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = mContext.getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
picturePath = "file://" + picturePath;
LogHelper.trace("Selected file uri:" + picturePath);
return Uri.parse(picturePath);
}
public static File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = MyPharmacy.getmInstance().getExternalCacheDir();
LogHelper.trace("createImageFile:" + storageDir.getAbsolutePath());
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
return image;
}
public static File openCamera(Activity mActivity, int requestCode) {
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
File photoFile = createImageFile();
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(MyPharmacy.getmInstance(), BuildConfig.APPLICATION_ID + ".provider", photoFile);
camera.putExtra(MediaStore.EXTRA_OUTPUT,
photoURI);
if (camera.resolveActivity(mActivity.getPackageManager()) != null) {
mActivity.startActivityForResult(camera, requestCode);
return photoFile;
}
}
} catch (IOException ex) {
}
return null;
}
public static File openCamera(Fragment fragment, int requestCode) {
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
File photoFile = createImageFile();
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(MyPharmacy.getmInstance(), BuildConfig.APPLICATION_ID + ".provider", photoFile);
camera.putExtra(MediaStore.EXTRA_OUTPUT,
photoURI);
if (camera.resolveActivity(fragment.getActivity().getPackageManager()) != null) {
fragment.startActivityForResult(camera, requestCode);
return photoFile;
}
}
} catch (IOException ex) {
}
return null;
}
然后使用下面的代码通过改造上传它
public static void uploadImage(final String filePath,
final String token,
final APIResponseCallback<UploadResponse> cb) {
if (filePath != null && !filePath.isEmpty()) {
final File file = new File(filePath);
if (file.exists()) {
Glide.with(App.getmInstance().getContext()).load(file).asBitmap().override(700, 700).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
OutputStream os = null;
try {
String realPath = StorageHelper.getRealPathFromUri(App.getmInstance().getContext(), Uri.fromFile(file));
os = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, os);
os.flush();
os.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
if (file != null) {
// creates RequestBody instance from file
RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);
// MultipartBody.Part is used to send also the actual filename
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
// adds another part within the multipart request
String descriptionString = "Image";
RequestBody description = RequestBody.create(okhttp3.MultipartBody.FORM, descriptionString);
// executes the request
ServiceGenerator.getAPIServices().uploadImage(body, description, token).enqueue(new Callback<UploadResponse>() {
@Override
public void onResponse(Call<UploadResponse> call, Response<UploadResponse> response) {
if (response.isSuccessful()) {
cb.onSuccess(response.body());
} else {
checkError(response, token);
}
}
@Override
public void onFailure(Call<UploadResponse> call, Throwable t) {
cb.onError(t);
}
});
}
}
});
}
}
}
API接口是:
@Multipart
@POST("Uploads/image/upload")
Call<UploadResponse> uploadImage(@Part MultipartBody.Part file,
@Part("description") RequestBody description,
@Query("access_token") String token);
此外,您可以将这些代码转换为 kotlin 等效代码。