【问题标题】:Getting a bitmap from gallery using Delphi via JNI, fails with Delphi XE7, works with Delphi XE6通过 JNI 使用 Delphi 从图库中获取位图,使用 Delphi XE7 失败,使用 Delphi XE6
【发布时间】:2014-10-18 11:42:01
【问题描述】:

我正在使用 jni 从 android 库中使用 Delphi XE7 获取位图。相同的代码,使用 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.

Image1.Bitmap.Assign(surface) 中的代码失败,我收到 Fragmentation 类错误。

Delphi XE7 中的哪些更改导致了此错误?

【问题讨论】:

  • TBitmapsurface 应该在 try .. finally 块之前创建
  • 确切的错误消息文本是什么?
  • ARC 不需要最后和免费通话
  • 即使在调试模式下,我也会出现黑屏。有一次,提出:“项目 ....apk 引发异常类段错误(11)”。使用 Delphi XE6,永不失败。

标签: android delphi bitmap java-native-interface


【解决方案1】:

试试:

TThread.Syncronize(nil, 程序 开始 Image1.Bitmap.Assign(表面); 结尾 );

【讨论】:

  • 它适用于平板电脑,如三星 Galaxy Tab S,但在手机上失败,如三星 S4。但如果应用程序是用 Delphi XE6 编译的,则适用于所有设备。用 XE7 或 XE8 编译,失败。
猜你喜欢
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 2014-10-30
  • 1970-01-01
相关资源
最近更新 更多