2011-06-25 76 views

回答

0

做到這一點,最簡單的(和最可靠)的方法是嘗試打開和decode它作爲一個Bitmap - 是這樣的:

String filePath;// assign the file to the path 
Bitmap img = BitmapFactory.decodeFile(filePath); 
if (img == null) { 
    // invalid image file 
} 
else { 
    // valid image file 
} 

它不漂亮,但它的工作原理。

+0

還好,但有沒有辦法做到這一點不開裂開文件?就像如果我甚至可以從操作系統得到支持的文件擴展名列表,這將是好的...我只是不想硬編碼「JPG」,「BMP,」GIF「,」PNG「 – user645402

+0

我不知道 – Haphazard

1

實際上有更好的方法(儘管仍然使用BitmapFactory)。你可以使用相同的工廠,但不能讀取位圖本身(當然這要快得多)。另外有一個選項參數decodeFile版本,你可以指定它應該只讀位圖大小(當然類型),而不是位圖本身:

String filePath;// assign the file to the path 
BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
Bitmap img = BitmapFactory.decodeFile(filePath, options); 
if (img == null) { 
    // invalid image file 
} 
else { 
    // valid image file 
} 
+0

hm ...有趣的是,這是更好的,但它仍然有破解打開文件...更好,但... – user645402

+0

沒有辦法檢查它沒有閱讀文件:)(除非你依靠擴展名)...我認爲這是最快的方式.. –

相關問題