我有一个需求,需要将现有的图像文件扩展名更改为其他内容并保存在设备上。
当我将.jpg扩展名更改为.xyz之类的东西时,安卓无法将此图像文件识别为图像文件,并且无法打开相同的文件。甚至相册也无法检测到此文件。
现在如何使安卓将此文件识别为图像文件?
-谢谢&问候, Manju
我有一个需求,需要将现有的图像文件扩展名更改为其他内容并保存在设备上。
当我将.jpg扩展名更改为.xyz之类的东西时,安卓无法将此图像文件识别为图像文件,并且无法打开相同的文件。甚至相册也无法检测到此文件。
现在如何使安卓将此文件识别为图像文件?
-谢谢&问候, Manju
将文件路径转换为URI:
File file = new File(path + File.pathSeparator + filename);
Uri uri = Uri.fromFile(file);
然后,使用内容解析器获取文件MIME类型:
String mimeType = getContentResolver().getType(uri);
您可以通过解码为位图来检查,只有图像文件才能解码为位图。
Bitmap bmp = BitmapFactory.decodeFile(filePath);
if(null!=bmp)
{
//image file
} else {
not image file.
}
String fileName="/storage/emulated/0/JP2/20131219_120037_16.icy";
File file = new File(fileName);
if(file!=null && file.exists()){
Log.d("Manju ==>","image file: "+fileName+", does exist");
}else{
Log.e("Manju ==>","image file: "+fileName+", doesnot exist !!");
}
Uri uri=Uri.fromFile(file);
String mimeType = getContentResolver().getType(uri);
Log.d("Manju ==>","mime Type: "+mimeType);
Bitmap bmp = BitmapFactory.decodeFile(fileName);
if(null!=bmp){
//image file
Log.d("Manju ==>","image file valid, height: "+bmp.getHeight()+", width: "+bmp.getWidth());
} else {
Log.d("Manju ==>","image file invalid !!!");
}
file.getAbosutePath().endWith(".jpg|.xyz)
可以使用模式匹配。 - Raghunandan