gpt4 book ai didi

java - 第一次无法上传图片,第二次就可以了

转载 作者:行者123 更新时间:2023-11-30 05:05:48 25 4
gpt4 key购买 nike

我正在从我的代码上传任何类型的文件,但问题是当我第一次运行我的代码并尝试上传图像时,图像当时没有上传。如果我先上传文件,例如一个 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();
}
}

最佳答案

据我所知,您正在从 Fragment 调用 requestPermissions 函数。当您使用 ActivityCompat.requestPermissions 请求用户许可时,onRequestPermissionsResult 不会在 Fragment 中被调用。 Here is a similar problem这表明 FragmentonRequestPermissionsResult 没有被调用,而调用 ActivityonRequestPermissionsResult 被调用。我认为这也是您的情况。

现在要摆脱这个问题,你需要使用

requestPermissions(new String[]{Manifest.permission.CAMERA}, STORAGE_PERMISSION_CODE);

代替

ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, STORAGE_PERMISSION_CODE);

不要在您的 fragment 中使用ActivityCompat.requestPermissions,因为这将调用您调用ActivityonRequestPermissionResult > 正在启动 Fragment

希望对您有所帮助。

关于java - 第一次无法上传图片,第二次就可以了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54624171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com