在一個應用程式中,我通過 Android 圖庫選擇照片。因為我想一次選擇多張照片,所以我使用了該JIntent功能。現在,一切正常,我想將選定的照片加載到TMSFMXtableview. 為此,我需要將照片JNet_Uri從TBitmap. 最好的方法是什么?
這是我想用來將照片加載到的代碼TMSFMXtableview:
function TfmMain.OnActivityResult(RequestCode: Integer; ResultCode: Integer; Data: Jintent): Boolean;
var
I,count: Integer;
ImageUri : Jnet_Uri;
Fid : Integer;
bitmap : Tbitmap;
begin
Result := False;
TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, FmessageSubscriptionID);
FmessageSubscriptionID := 0;
if requestcode=LibraryRequestCode then
begin
if ResultCode=TJActivity.JavaClass.RESULT_OK then
begin
if assigned(data) then
begin
Count := data.getClipData.getItemCount;
for I := 0 to count-1 do
begin
ImageUri := data.getClipData.getItemAt(i).getUri;
bitmap := //convert the imageuri to a Tbitmap
tv2.Items.Add;
FID := tv2.Items.Count-1;
tv2.Items.Items[FID].Bitmap.Assign(bitmap);
tv2.Items.Items[FID].Caption := 'NEW';
end;
end;
end
else
if ResultCode= TJActivity.JavaClass.RESULT_CANCELED then
begin
end;
result := true;
end;
end;
uj5u.com熱心網友回復:
恐怕沒有那么簡單。此代碼(來自 Dave Nottage 提供的示例)可以幫助您。AddAFile將 aJnet_URI作為輸入引數并將該檔案保存到GetPublicPath. 如果您愿意,您當然可以選擇將其保存array of bytes到流中,然后從該流中加載位圖。
function TfrmChat.AddAFile(const AURI: Jnet_Uri): TBitMap;
var
LSelectedFile: TSelectedFile;
LProjection: TJavaObjectArray<JString>;
LCursor: JCursor;
LURI: Jnet_Uri;
LInput: JInputStream;
LJavaBytes: TJavaArray<Byte>;
LBytes: TBytes;
FileName: String;
APath, AFileName: String;
lStream: TMemoryStream;
begin
if AURI = nil then
EXIT;
LSelectedFile.RawPath := JStringToString(AURI.toString);
LSelectedFile.DecodedPath := JStringToString(TJnet_Uri.JavaClass.decode(AURI.toString));
LProjection := TJavaObjectArray<JString>.Create(1);
try
LProjection[0] := TJMediaStore_MediaColumns.JavaClass.DISPLAY_NAME;
LCursor := TAndroidHelper.Context.getContentResolver.query(AURI, LProjection, nil, nil, nil);
finally
LProjection.Free;
end;
if LCursor = nil then
EXIT;
try
if LCursor.moveToFirst then
LSelectedFile.DisplayName := JStringToString(LCursor.getString(0));
FileName := lSelectedFile.DisplayName;
LURI := TJnet_Uri.JavaClass.parse(AURI.toString); // raw path again
LInput := TAndroidHelper.Context.getContentResolver.openInputStream(LURI);
// copy java input stream to array of bytes
LJavaBytes := TJavaArray<Byte>.Create(LInput.available);
try
LInput.read(LJavaBytes, 0, LJavaBytes.Length);
SetLength(LBytes, LJavaBytes.Length);
Move(LJavaBytes.Data^, LBytes[0], LJavaBytes.Length);
finally
LJavaBytes.Free;
end;
// write the bytes to a local file
// APath := System.IOUtils.TPath.GetPublicPath;
// AFileName := TPath.Combine(aPath, FileName);
// TFile.WriteAllBytes(AFileName, LBytes);
// create a bitmap to return
lStream := TMemoryStream.Create;
try
lStream.WriteBuffer(LBytes[0], length(lBytes));
Result := TBitmap.CreateFromStream(lStream);
finally
lStream.Free;
end;
finally
LCursor.Close;
end;
end;
[編輯] 我已將程序更改為回傳 TBitmap 的函式。你可以像這樣使用它:
var
lBitMap: TBitMap;
begin
lBitMap := AddFile(ImageUri);
try
// do stuff with the bitmap
finally
lBitMap.Free;
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/474104.html
上一篇:程式物件如何改變_self?
