I am facing the issues in picking the file path of the file, i searched for all over the stack overflow but the issue is not solved .Code for picking the files from the device is shown below.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//intent.addFlags(ST)
startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), PICK_FILE_REQUEST);
The picked files from intent are get by
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PICK_FILE_REQUEST) {
if (data != null) {
//no data present
Uri uri = data.getData();
String filePath = data.getData().getPath();
// String path = uri.getPath();
file = new File(filePath);
String name = getContentName(getContentResolver(), uri);
try {
InputStream inStream = getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmap);
attachFile.addView(imageView);
TextView textView = new TextView(this);
textView.setText(name);
attachFile.addView(textView);
return;
}
}
}
}
In the above code file path is obtained by the ` String filePath = data.getData().getPath(); but when uploading the files to the server exception is thrown like invalid document and file .How to get the correct path of the file from the uri ?
But the name of the file is picked by using
`public static String getContentName(ContentResolver resolver, Uri uri) {
Cursor cursor = resolver.query(uri, null, null, null, null);
cursor.moveToFirst();
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
if (nameIndex >= 0) {
return cursor.getString(nameIndex);
} else {
return null;
}
}`
How to get the correct file path for the picked file ?