在android下,当我们从图库中选择一个图像时,我们不会收到带扩展名的文件名,而是使用uri而我们必须使用contentprovider将文件复制到我们的本地目录中。
现在因为文件没有任何扩展名,如何在Tbitmap对象中加载它?我尝试但收到错误:无法加载图片。
答案 0 :(得分:1)
好的,我确实喜欢这个:
function DetectImageExtension(const aFileName: string): String;
var aFileStream: TFileStream;
aFirstBytes: Tbytes;
begin
aFileStream := TFileStream.Create(aFileName, fmOpenRead);
try
if aFileStream.Size < 8 then exit('');
SetLength(aFirstBytes, 8);
aFileStream.ReadBuffer(aFirstBytes[0], length(aFirstBytes));
if (aFirstBytes[0] = $FF) and
(aFirstBytes[1] = $D8) then result := 'jpg' // ÿØ
else if (aFirstBytes[0] = $89) and
(aFirstBytes[1] = $50) and
(aFirstBytes[2] = $4E) and
(aFirstBytes[3] = $47) and
(aFirstBytes[4] = $0D) and
(aFirstBytes[5] = $0A) and
(aFirstBytes[6] = $1A ) and
(aFirstBytes[7] = $0A) then result := 'png' // .PNG....
else if (aFirstBytes[0] = $47) and
(aFirstBytes[1] = $49) and
(aFirstBytes[2] = $46) then result := 'gif' // GIF
else if (aFirstBytes[0] = $42) and
(aFirstBytes[1] = $4D) then result := 'bmp' // BM
else result := '';
finally
aFileStream.Free;
end;
end;