如何限制用户从图库中仅选择JPG图像?

Sky*_*net 4 android android-gallery android-selector

在我的应用程序中,我有一个选项,允许用户从库中选择一个图像并在应用程序中使用它.但是我想限制用户只选择带.jpg扩展名的图像.我如何实现这一目标?

到目前为止,我已经尝试了以下方法,但每种类型的图像都可以使用这种方法:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, RESULT_LOAD_IMAGE);
Run Code Online (Sandbox Code Playgroud)

Sha*_*Adi 7

1.您可以使用

intent.setType("image/jpeg");
Run Code Online (Sandbox Code Playgroud)

要么

intent.setType("image/jpg");
Run Code Online (Sandbox Code Playgroud)

2.或者您可以使用onActivityResult来检查文件:

filePath.endsWith(".jpg")
Run Code Online (Sandbox Code Playgroud)

如果文件是.jpg或.jpeg类型,那么只实现你的逻辑,否则Toast文件应该是.jpg格式.