简体   繁体   中英

Getting a bitmap from gallery using Delphi via JNI, fails with Delphi XE7, works with Delphi XE6

I'm getting a bitmap using Delphi XE7 from android gallery using jni. Same code, worked using Delphi XE6:

unit home;

interface

uses
  FMX.Platform.Android, Androidapi.Helpers, Androidapi.JNI.App, Androidapi.JNI.GraphicsContentViewText, Androidapi.JNIBridge,
  FMX.Helpers.Android, Androidapi.JNI.Net, Androidapi.JNI.Provider, Androidapi.JNI.Media, Androidapi.JNI.JavaTypes,
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.Messaging,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  FMX.Objects, FMX.Surfaces, Data.DB, MemDS, DBAccess, MyAccess, Strutils,
  FMX.Layouts, FMX.Memo;

type
  TForm1 = class(TForm)
    ToolBar1: TToolBar;
    SpeedButton1: TSpeedButton;
    Label1: TLabel;
    Image1: TImage;
    procedure SpeedButton1Click(Sender: TObject);
  private
    { Private declarations }
    const ScanRequestCode = 0;
    var FMessageSubscriptionID: Integer;
    procedure HandleActivityMessage(const Sender: TObject; const M: TMessage);
    function OnActivityResult(RequestCode, ResultCode: Integer; Data: JIntent): Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID}

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  Intent: JIntent;
begin
  FMessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification,
    HandleActivityMessage);
  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_GET_CONTENT);
  Intent.setType(StringToJSTring('image/*'));
  SharedActivity.startActivityForResult(Intent,0);
end;

procedure TForm1.HandleActivityMessage(const Sender: TObject; const M: TMessage);
begin
  if M is TMessageResultNotification then
    OnActivityResult(TMessageResultNotification(M).RequestCode, TMessageResultNotification(M).ResultCode,
      TMessageResultNotification(M).Value);
end;

function TForm1.OnActivityResult(RequestCode, ResultCode: Integer; Data: JIntent): Boolean;
var
  uri: Jnet_Uri;
  bitmap: JBitmap;
  surface: TBitmapSurface;
begin

  TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, FMessageSubscriptionID);
  FMessageSubscriptionID := 0;

  if Assigned(Data) then
    begin
    try
      uri:=Data.getData;
      bitmap := TJImages_Media.JavaClass.getBitmap(SharedActivity.getContentResolver, uri);
      surface := TBitmapsurface.Create;
      JBitMapToSurface(bitmap,surface);
      // Fails here in Delphi XE7
      //Image1.Bitmap.Assign(surface);
    finally
        surface.Free;
        Result := true;
    end;

    end
    else Result := false;

end;

end.

Code fails in Image1.Bitmap.Assign(surface), I get a Fragmentation class error.

What's changed in Delphi XE7 to cause this error?

Try:

TThread.Syncronize( nil, procedure begin Image1.Bitmap.Assign( surface ); end );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM