我正在通过图库,相机和文件管理器实现文件上传概念。 这是代码
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
suppdocs.setText(strPath);
} else if (requestCode == 2) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
Uri tempUri = getImageUri(getApplicationContext(), bm);
File finalFile = new File(getRealPathFromURI(tempUri));
suppdocs.setText(strPath);
} else if (requestCode == 3) {
Uri uri = data.getData();
strPath= getPath(getApplicationContext(),uri);
suppdocs.setText(strPath);
}
}
}
这是从filemanager获取所选文件路径的代码
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/"
+ split[1];
}
} else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
} else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{split[1]};
return getDataColumn(context, contentUri, selection,
selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
//这是将文件发送到服务器的代码
private void senddata1(final String path) {
if(common.isNetworkAvailable(getApplicationContext())) {
try {
Log.d("TestTag","filename : "+path+" file : "+strfile);
progressBar2.setVisibility(View.VISIBLE);
submit_btn.setVisibility(View.GONE);
File file = new File(path);
// Parsing any Media type file
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName());
RequestBody strgid = RequestBody.create(MediaType.parse("text/plain"),gid);
RequestBody key = RequestBody.create(MediaType.parse("text/plain"),strKey);
APIService getResponse = apiclient.imgUpload().create(APIService.class);
Call<ImageUpload> call = getResponse.uploadFile(fileToUpload,filename);
call.enqueue(new Callback<ImageUpload>() {
@Override
public void onResponse(Call<ImageUpload> call, Response<ImageUpload> response) {
progressBar2.setVisibility(View.GONE);
submit_btn.setVisibility(View.VISIBLE);
if (response.isSuccessful()) {
String message=response.body().getMessage();
String type=response.body().getType();
}
}
@Override
public void onFailure(Call<ImageUpload> call, Throwable t) {
Log.d("TestTag","throwable : "+t);
common.showtoast("Failed to upload image.",getApplicationContext());
}
});
} catch (Exception e) {
Log.d("TestTag","exception : "+e);
}
}
}
我试图通过相机,画廊和filemanager将图像上载到服务器,但其正常工作,但是当我通过filemanage选择pdf文件时,出现SSL异常。
预期输出: 如果我通过文件管理器选择pdf文件,则应正确上传pdf文件