I am trying to open files by using Intent.ACTION_GET_CONTENT like in this post:
Android opening a file with ACTION_GET_CONTENT results into different Uri's
But here is just a solution how to get the filename with different SDKs/Folders and not for different devices. Also the Intent to get the Uri stays the same.
I want to open .png files.
Uri.getPath() for the both devices are (both .png files stored in the download folder):
Samsung S3 Tablet (Android 8.0): /document/559
Samsung Galaxy S7 (Android 8.0): /document/raw:/storage/0/emulated/Download/Karte1.png
Therefore the problem is, if i initialise an InputStream with
getActivity().getContentResolver().openInputStream(uri)
its not working for the Tablet.
Here is the Intent code snippet:
public void browseClick() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/png");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent,
getString(R.string.choose_floorPlan)),REQUEST_CODE_OPEN_FILE);
} catch (Exception ex) {
System.out.println("browseClick :"+ex);
}
}
OnActivityResult:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != REQUEST_CODE_OPEN_FILE) {
return;
}
if ((resultCode == RESULT_OK) && (data != null)) {
try {
selectedMap = data.getData();
String filename = FileHelper.getUriName(selectedMap,
getActivity());
textMapDir.setText(getString(R.string.placeholder_
folder_begin, filename));
textMapDir.setText(selectedMap.getPath());
} catch (Exception e) {
e.printStackTrace();
}
}
}
In this method im using the uri and openInputStream(uri)
private boolean createFiles(String pathProjectDir, Uri selectedMap) {
if (!ExistsDir(pathProjectDir)) { //it should be possible to add more floors/buildings to an existing project
if (getAndCreateDir(pathProjectDir).exists()) {
InputStream excelFile;
InputStream mapFile;
try {
excelFile = Objects.requireNonNull(getActivity()).getAssets().open(EXCEL_FILE_NAME);
mapFile = getActivity().getContentResolver().openInputStream(selectedMap);
if (FileHelper.getMimeType(selectedMap.getPath()).equals("application/pdf")) {
// some code
} else if (FileHelper.getMimeType(selectedMap.getPath()).equals("image/png")) {
if ((copyFile(excelFile, getExcelPath(pathProjectDir)))
&& (copyFile(mapFile, getMapPathPNG(pathProjectDir)))) {
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getActivity(), getString(R.string.excel_file_unable_to_create), Toast.LENGTH_LONG).show();
return false;
}
}
Toast.makeText(getActivity(), getString(R.string.project_dir_was_not_created), Toast.LENGTH_LONG).show();
return false;
}
Toast.makeText(getActivity(), getString(R.string.project_dir_exisitng), Toast.LENGTH_LONG).show();
return false;
}
Thats the getMimeType() method, where i check the MIME from the uri, that could be the problem:
public static String getMimeType(String path) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(path);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
This is the copyFile() method where I copy the InputStream:
public static Boolean copyFile(InputStream isFile, String pathOutput) {
try {
FileOutputStream fos = new FileOutputStream(new File(pathOutput));
copyFileBuffer(isFile, fos);
isFile.close();
fos.flush();
fos.close();
return true;
} catch (IOException io) {
io.printStackTrace();
}
return false;
}
To complete the code here is the copyFileBuffer() method, but this should be fine:
public static Boolean copyFile(InputStream isFile, String pathOutput) {
try {
FileOutputStream fos = new FileOutputStream(new File(pathOutput));
copyFileBuffer(isFile, fos);
isFile.close();
fos.flush();
fos.close();
return true;
} catch (IOException io) {
io.printStackTrace();
}
return false;
}