在 android 中选择特定类型的文件
Selecting a specific type of file in android
我正在尝试制作一个应用程序,用户可以使用 android 设备的文件管理器 select 一个文件。这是我正在使用的代码:
boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat)
{
Intent uploadIntent = new Intent();
uploadIntent.setType("*/*");
uploadIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(uploadIntent, REPORT_USING_FILE_MANAGER);
}
else
{
Intent uploadIntent = new Intent(Intent.ACTION_GET_CONTENT);
uploadIntent.setType("*/*");
startActivityForResult(uploadIntent, REPORT_USING_FILE_MANAGER);
}
我只希望用户能够 select 图像文件(.jpg、.png 等)或 .pdf 文件。如何设置此限制?
您正在使用 loadIntent.setType("*/*");
选择所有类型的文件。
您必须使用 MIME 类型对其进行过滤。
例如,如果您想选择 jpeg 文件,那么
loadIntent.setType("image/jpeg");
部分MIME类型如下,
image/jpeg
audio/mpeg4-generic
text/html
audio/mpeg
audio/aac
audio/wav
audio/ogg
audio/midi
audio/x-ms-wma
video/mp4
video/x-msvideo
video/x-ms-wmv
image/png
image/jpeg
image/gif
.xml ->text/xml
.txt -> text/plain
.cfg -> text/plain
.csv -> text/plain
.conf -> text/plain
.rc -> text/plain
.htm -> text/html
.html -> text/html
.pdf -> application/pdf
.apk -> application/vnd.android.package-archive
如果您有任何问题,请告诉我。
您需要为图像文件
实施 "photoPickerIntent"
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
之后应完成以下操作:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
switch (requestCode)
{
case SELECT_PHOTO:
if (resultCode == RESULT_OK)
{
Uri selectedImage = intent.getData();
System.out.println(selectedImage.toString() + "-" + selectedImage.getPath());
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
}
break;
default:
break;
}
}
我正在尝试制作一个应用程序,用户可以使用 android 设备的文件管理器 select 一个文件。这是我正在使用的代码:
boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat)
{
Intent uploadIntent = new Intent();
uploadIntent.setType("*/*");
uploadIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(uploadIntent, REPORT_USING_FILE_MANAGER);
}
else
{
Intent uploadIntent = new Intent(Intent.ACTION_GET_CONTENT);
uploadIntent.setType("*/*");
startActivityForResult(uploadIntent, REPORT_USING_FILE_MANAGER);
}
我只希望用户能够 select 图像文件(.jpg、.png 等)或 .pdf 文件。如何设置此限制?
您正在使用 loadIntent.setType("*/*");
选择所有类型的文件。
您必须使用 MIME 类型对其进行过滤。
例如,如果您想选择 jpeg 文件,那么
loadIntent.setType("image/jpeg");
部分MIME类型如下,
image/jpeg
audio/mpeg4-generic
text/html
audio/mpeg
audio/aac
audio/wav
audio/ogg
audio/midi
audio/x-ms-wma
video/mp4
video/x-msvideo
video/x-ms-wmv
image/png
image/jpeg
image/gif
.xml ->text/xml
.txt -> text/plain
.cfg -> text/plain
.csv -> text/plain
.conf -> text/plain
.rc -> text/plain
.htm -> text/html
.html -> text/html
.pdf -> application/pdf
.apk -> application/vnd.android.package-archive
如果您有任何问题,请告诉我。
您需要为图像文件
实施 "photoPickerIntent"Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
之后应完成以下操作:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
switch (requestCode)
{
case SELECT_PHOTO:
if (resultCode == RESULT_OK)
{
Uri selectedImage = intent.getData();
System.out.println(selectedImage.toString() + "-" + selectedImage.getPath());
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
}
break;
default:
break;
}
}