我正在尝试使用Intent.ACTION_GET_CONTENT打开文件,如这篇文章中:
Android 打开一个文件,将ACTION_GET_CONTENT结果放入不同的 Uri 中
但这只是一个解决方案,如何使用不同的SDK/文件夹而不是不同的设备获取文件名。此外,获取 Uri 的意图保持不变。
我想打开.png文件。
两个设备的 Uri.getPath(( 都是(都存储在下载文件夹中的.png文件(:
Samsung S3 Tablet (Android 8.0): /document/559
Samsung Galaxy S7 (Android 8.0): /document/raw:/storage/0/emulated/Download/Karte1.png
因此,问题是,如果我使用
getActivity().getContentResolver().openInputStream(uri)
它不适用于平板电脑。
以下是 Intent 代码片段:
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);
}
}
活动结果:
@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();
}
}
}
在此方法中,im 使用 uri 和 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;
}
这是getMimeType((方法,我从uri检查MIME,这可能是问题所在:
public static String getMimeType(String path) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(path);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
这是我复制输入流的copyFile((方法:
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;
}
要完成这里的代码是copyFileBuffer((方法,但这应该没问题:
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;
}
你必须从 uri 获取真正的路径。下面是它的代码。我从堆栈溢出的帖子中找到了它,但我没有它的链接.因此,功劳归于发布它的人。传递上下文和 uri 并获取真正的路径
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getPathFromUri(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
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];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
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);
}
// MediaProvider
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);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}