Reputation: 81
I have uploaded a PDF file to the server but I am not able to get the path from sd card in Marshmallow. Could someone help and figure out what the issue is?
but_browse
is the button for browsing the files and after browsing I have given an upload button to upload the PDF to the server.
but_browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//sets the select file to all types of files
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf");
//intent.putExtra("browseCoa", itemToBrowse);
//Intent chooser = Intent.createChooser(intent, "Select a File to Upload");
try {
//startActivityForResult(chooser, FILE_SELECT_CODE);
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), 1);
} catch (Exception ex) {
System.out.println("browseClick :" + ex);//android.content.ActivityNotFoundException ex
}
}
});
onActivityResult()
method:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
// if (requestCode == 1) {
Uri uri = data.getData();
String uriString = uri.toString();
// uploadedFileName = file.getName().toString();
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
File myFile = new File(uriString);
// selectedFilePath2 = PathUtils.getPath(getActivity(), uri );
System.out.println(">>>>>>>>>>>>>>>>>>>>selectedFilePath2"+ getApplicationContext().getFilesDir().getPath());
// System.out.println(">>>>>>>>>>>>>>>>>>>>file.getName()" + file.getName());
// file_name = file.getName();
// Log.i("", "File : " + file.getName());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
File[] externalCacheDirs = getActivity().getExternalCacheDirs();
String[] f = getExternalStorageDirectories();
System.out.println(">>>>>>>>>>>>>>>>>>>>selectedFilePath3" + f );
for (File file : externalCacheDirs) {
if (Environment.isExternalStorageRemovable(file)) {
// Path is in format /storage.../Android....
// Get everything before /Android
selectedFilePath2 = file.getPath().split("/Android")[0];
System.out.println(">>>>>>>>>>>>>>>>>>>>selectedFilePath2" + selectedFilePath2 );
break;
}
}
selectedFilePath2 = myFile.getAbsolutePath();
System.out.println(">>>>>>>>>>>>>>>>>>>>selectedFilePath2" + selectedFilePath2 );
}
}
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N) {
File myFile = new File(uriString);
selectedFilePath2 = PathUtils.getPath(getActivity(), uri );
System.out.println(">>>>>>>>>>>>>>>>>>>>selectedFilePath2" + selectedFilePath2);
System.out.println(">>>>>>>>>>>>>>>>>>>>selectedFilePath2"+ getApplicationContext().getFilesDir().getPath());
// System.out.println(">>>>>>>>>>>>>>>>>>>>file.getName()" + file.getName());
// file_name= file.getName();
// Log.i("", "File : " + file.getName());
} else {
file = new File(uri.getPath().toString());
selectedFilePath2 = Vis_FilePath.getPath(getActivity(), uri) ;
System.out.println(">>>>>>>>>>>>>>>>>>>>file.getName()" + file.getName());
file_name= file.getName();
Log.i("", "File : " + file.getName());
}
}
}
Upload to server:
private void uploadpdf() throws IOException {
String charset = "UTF-8";
byte[] data = null;
File filessss = new File(selectedFilePath2);
try {
data = FileUtils.readFileToByteArray(filessss);
pdf = Base64.encodeToString(data, Base64.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
final ProgressDialog loading = ProgressDialog.show(getActivity(),"Uploading...","Please wait...",false,false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.URL_CV,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
//Dismissing the progress dialog
loading.dismiss();
//Showing toast message of the response
Toast.makeText(getActivity(), s , Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Dismissing the progress dialog
loading.dismiss();
//Showing toast
Toast.makeText(getActivity(), volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
//Creating parameters
Map<String,String> params = new Hashtable<String, String>();
SharedPreferences prefs = getActivity().getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
String userid = prefs.getString("userId","");
//Adding parameters
params.put("user_id",userid);
params.put("attachment",pdf);
//returning parameters
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request to the queue
requestQueue.add(stringRequest);
}
Upvotes: 0
Views: 296
Reputation: 81
Did you checked permission on manifest file? I had similar problem before because I didn't wrote these.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
And used PackageManager to check required permission are granted.
I get some help from here : https://www.captechconsulting.com/blogs/runtime-permissions-best-practices-and-how-to-gracefully-handle-permission-removal
Upvotes: 1