我正在从代码中上传任何类型的文件,但是问题是当我第一次运行我的代码并尝试上传图像时,该图像当时未上传。如果我先上传文件,例如一个PDF文件,它已成功上传,此后,当我上传图像时,它也成功上传了。我对这个问题一无所知。请帮忙。
这是上传图像/文件的代码。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
showFileChoser();
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CAMERA)) {
Toast.makeText(getActivity(), "App requires Phone permission.\nPlease allow that in the device settings.", Toast.LENGTH_LONG).show();
}
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, STORAGE_PERMISSION_CODE);
}
} else {
showFileChoser();
}
还有showFileChoser
函数。
private void showFileChoser() {
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select document"), PICK_DOCUMENT);
}
onActivityResult
。
// Handling the ima chooser activity result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PDF_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
// uploadMultipart();
try {
execMultipartPost();
} catch (Exception e) {
e.printStackTrace();
}
}
}
onRequestPermissionsResult
是...
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//Checking the request code of our request
if (requestCode == STORAGE_PERMISSION_CODE) {
//If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Displaying a toast
Toast.makeText(getActivity(), "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
} else {
//Displaying another toast if permission is not granted
Toast.makeText(getActivity(), "Oops you just denied the permission", Toast.LENGTH_LONG).show();
}
}
}
文件上传功能如下。
private void execMultipartPost() throws Exception {
RequestBody requestBody;
pBar.setVisibility(View.VISIBLE);
final SessionManager session = new SessionManager(getActivity());
final HashMap<String, String> loggedDetail = session.getLoggedDetail();
HashMap<String, String> params = new HashMap<String, String>();
String api_token = loggedDetail.get("api_token");
if (filePath != null) {
String path = FilePath.getPath(getActivity(), filePath);
requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("identity_file", path)
.addFormDataPart("training", edtTraining.getText().toString())
.addFormDataPart("user_id", loggedDetail.get("id"))
.addFormDataPart("experience", experience)
.addFormDataPart("skills", edtSkills.getText().toString())
.addFormDataPart("address_file", "")
.addFormDataPart("cv_file", "")
.addFormDataPart("dbs_file", "")
.build();
}
}
答案 0 :(得分:0)
据我所知,您正在从requestPermissions
调用Fragment
函数。当您使用ActivityCompat.requestPermissions
向用户请求权限时,onRequestPermissionsResult
中没有调用Fragment
。 Here is a similar problem表示未调用Fragment
的{{1}},而正在调用调用的onRequestPermissionsResult
的{{1}}。我认为这也是您的情况。
现在要摆脱这个问题,您需要使用
Activity
代替
onRequestPermissionsResult
请勿在{{1}}中使用requestPermissions(new String[]{Manifest.permission.CAMERA}, STORAGE_PERMISSION_CODE);
,因为这将调用正在启动ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, STORAGE_PERMISSION_CODE);
的呼叫ActivityCompat.requestPermissions
的{{1}}。
希望有帮助。