我正在使用文件提供程序从相机上传照片,因为从API 26开始,我们需要使用文件提供程序。我在应用程序中使用了文件提供程序。我尝试上传图片,但是每次获取文件时都找不到异常。我的照片不是从Oreo和Pie中的相机上传的。现在,它显示文件在每个设备中都找不到异常。
代码:
private void openCameraIntent() {
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (pictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
return;
}
photoUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", photoFile);
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(pictureIntent, REQUEST_IMAGE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION && grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Thanks for granting Permission", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE) {
if (resultCode == RESULT_OK) {
imageView.setImageURI(Uri.parse(imageFilePath));
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "You cancelled the operation", Toast.LENGTH_SHORT).show();
}
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = "deep_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
imageFilePath = image.getAbsolutePath();
return image;
}
private void execMultipartPost() throws Exception {
Toast.makeText(DemoCam.this, "hello", Toast.LENGTH_SHORT).show();
String api_token;
String path = photoUri.getPath();
// "/mnt/sdcard/FileName.mp3"
RequestBody requestBody;
if (path != null) {
Toast.makeText(DemoCam.this, path, Toast.LENGTH_SHORT).show();
File file = new File(path);
Log.e("TAG", "execMultipartPost:file " + file);
String contentType = file.toURL().openConnection().getContentType();
fileBody = RequestBody.create(MediaType.parse(contentType), file);
String filename = "file_" + System.currentTimeMillis() / 1000L;
Log.e("TAG", "filename: " + fileBody);
requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("image_file", filename + ".jpg", fileBody)
.addFormDataPart("name", "eere")
.addFormDataPart("user_id", "346")
.addFormDataPart("email", "a@gmail.com")
.addFormDataPart("postal_code", "121001")
.addFormDataPart("phone", "7503366400")
.addFormDataPart("type", "S")
.addFormDataPart("address", "fefrfrf")
.addFormDataPart("gender", "M")
.addFormDataPart("skype_id", "hde.jfjf")
.build();
} else {
requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("image_file", "")
.addFormDataPart("name", "eere")
.addFormDataPart("user_id", "346")
.addFormDataPart("email", "a@gmail.com")
.addFormDataPart("postal_code", "121001")
.addFormDataPart("phone", "7503366400")
.addFormDataPart("type", "S")
.addFormDataPart("address", "fefrfrf")
.addFormDataPart("gender", "M")
.addFormDataPart("skype_id", "hde.jfjf")
.build();
}
okhttp3.Request request = new okhttp3.Request.Builder()
.url(Constants.EDIT_PROFILE_DATA)
.post(requestBody)
.addHeader("Authorization", "Bearer " + api_token)
.build();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(150, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(DemoCam.this, e.toString(), Toast.LENGTH_SHORT).show();
Log.e("TAG", "run:error " + e.toString());
e.printStackTrace();
}
});
}
@Override
public void onResponse(Call call, final okhttp3.Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
ResponseBody responseBody = response.body();
String content = responseBody.string();
Log.e("TAG", "advisor profile content: " + content);
} catch (Exception e) {
Toast.makeText(DemoCam.this, e.toString(), Toast.LENGTH_SHORT).show();
//pBar.setVisibility(View.GONE);
e.printStackTrace();
}
}
});
}
});
}