Home > Enterprise >  Convert Jnet_uri to Tbitmap in delphi
Convert Jnet_uri to Tbitmap in delphi

Time:05-11

In an app, I select photos through the Android gallery. As I want to select multiple photos at once, I use the JIntent function. Now, it all works, and I want to load the selected photos into a TMSFMXtableview. For this, I need to convert the photos from JNet_Uri to TBitmap. What is the best way to do that?

This is the piece of code I want to use to load the photos into the 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;

CodePudding user response:

It's not as simple as that, I'm afraid. This code (from examples provided by Dave Nottage) may assist you. AddAFile takes a Jnet_URI as an input param and saves that file to GetPublicPath. You can of course choose to save the array of bytes to a stream and then load the bitmap from that stream, if you wish.

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;

[Edit] I have changed the procedure into a function that returns a TBitmap. You can use it like this:

var
  lBitMap: TBitMap;
begin
  
  lBitMap := AddFile(ImageUri);
  try
    // do stuff with the bitmap
  finally
    lBitMap.Free;
  end;
  • Related