gpt4 book ai didi

android - 无法使用 OkHttp3 在文件管理器中选择文件

转载 作者:太空宇宙 更新时间:2023-11-03 10:37:41 25 4
gpt4 key购买 nike

我正在尝试创建一个应用程序,我必须在其中上传任何不超过 16-20 MB 的文件。我正在使用 OkHttpClient3,但我的主要问题是它将使用简单名称的文件,例如 a.mp4、b.jpg 等,但它不能选择像 hello world 12.mp4 这样的文件,代码应该是什么放在 RequestBody file_body = RequestBody.create(MediaType.parse(content_type),f).我的代码是。

public  void enable_button(){
choosefile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new MaterialFilePicker()
.withActivity(ShowStudent.this)
.withRequestCode(10)
.start();
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == 100 && (grantResults[0] == PackageManager.PERMISSION_GRANTED)){
enable_button();
}else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},100);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
if(requestCode == 10 && resultCode == RESULT_OK){
progress = new ProgressDialog(ShowStudent.this);
progress.setTitle("Uploading");
progress.setMessage("Please wait...");
progress.show();

Thread t = new Thread(new Runnable() {
@Override
public void run() {
File f = new File(data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH));
String content_type = getMimeType(f.getPath());
String file_path = f.getAbsolutePath();
OkHttpClient client = new OkHttpClient();
RequestBody file_body = RequestBody.create(MediaType.parse(content_type),f);
RequestBody request_body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("type",content_type)
.addFormDataPart("uploaded_file",file_path.substring(file_path.lastIndexOf("/")+1), file_body)
.build();
Request request = new Request.Builder()
.url("http://onsitesupport.info/diary/uploadproject.php")
.post(request_body)
.build();
try {
Response response = client.newCall(request).execute();
if(!response.isSuccessful()){
throw new IOException("Error : "+response);
}
progress.dismiss();
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
}
}
private String getMimeType(String path) {
String extension = MimeTypeMap.getFileExtensionFromUrl(path);
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}

我得到了这个异常

E/AndroidRuntime: FATAL EXCEPTION: Thread-3122
Process: com.kliff.studentteacherportal, PID: 29436
java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
at java.util.regex.Matcher.reset(Matcher.java:177)
at java.util.regex.Matcher.<init>(Matcher.java:90)
at java.util.regex.Pattern.matcher(Pattern.java:297)
at okhttp3.MediaType.parse(MediaType.java:51)
at com.kliff.studentteacherportal.ShowStudent$2.run(ShowStudent.java:100)
at java.lang.Thread.run(Thread.java:818)
E/WindowManager: android.view.WindowLeaked: Activity com.kliff.studentteacherportal.ShowStudent has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{26acab10 V.E..... R......D 0,0-684,322} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:458)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:278)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:311)
at com.kliff.studentteacherportal.ShowStudent.onActivityResult(ShowStudent.java:91)
at android.app.Activity.dispatchActivityResult(Activity.java:6294)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3873)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3920)
at android.app.ActivityThread.access$1300(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5706)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1033)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
W/MALI: glDrawArrays:714: [MALI] glDrawArrays takes more than 5ms here. Total elapse time(us): 5659

最佳答案

好的,我明白了,它不能选择带空格的文件,但我通过替换解决了我的问题

String mimeType= URLConnection.guessContentTypeFromName(f.getName()); 
RequestBody file_body = RequestBody.create(MediaType.parse(mimeType),f);`

关于android - 无法使用 OkHttp3 在文件管理器中选择文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40480874/

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