【问题标题】:Titanium android, receiving intent, how to access to the native file?Titanium android,接收intent,如何访问原生文件?
【发布时间】:2015-10-09 11:21:49
【问题描述】:

在我的 TiApp.xml 中,我得到了以下块:

<activity android:name=".TestActivity" android:label="@string/app_name" android:theme="@style/Theme.Titanium" android:configChanges="keyboardHidden|orientation|screenSize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
    <intent-filter android:label="@string/kmob.intent.menu.send">
        <data android:mimeType="*/*"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <action android:name="android.intent.action.SEND"/>
    </intent-filter>
</activity>

而且效果很好。我可以进入照片库,选择一张照片,然后我可以与我的应用程序共享照片。但在我的应用程序中,我遇到了问题。我需要访问文件名或至少它的扩展名。但我只有一个包含图像内容的 blob。

if (OS_ANDROID) {
    var intent = Ti.Android.currentActivity.getIntent();

    if (intent && intent.action == 'android.intent.action.SEND') {
        // Blob contains the content of the image (an application/octet-stream data). But there's no filename or filepath or file mimetype or whatever
        var blob = intent.getBlobExtra(Ti.Android.EXTRA_STREAM);

        // How can i access the native file or its filename ?
    }
}

如何访问本机文件或其文件名?或者至少是它的扩展? (这是我唯一需要的,因为我有文件内容。我可以通过我需要扩展名来创建一个包含内容的临时文件)

在文档中,我只能找到带有简单文本文件或仅显示图像内容的示例。他们从不访问本机文件或其文件名。

这里是intent和blob的字符串表示:

intent = {
    "action": "android.intent.action.SEND",
    "bubbleParent": true,
    "data": null,
    "type": "image/jpeg", // NOTE : This value is not always the same. For an android 4.2.x, it's "image/jpeg", on android 4.1.x, it's "image/*".
    "apiName": "Ti.Android.Intent",
    "flags": 50331649
};

blob = {
    "height": 0,
    "bubbleParent": true,
    "type": 2,
    "mimeType": "application/octet-stream",
    "apiName": "Ti.Blob",
    "nativePath": null,
    "file": null,
    "text": "<IMAGE CONTENT AS STRING>"
}

【问题讨论】:

    标签: javascript android-intent titanium titanium-android


    【解决方案1】:

    您应该尝试读取 blob 对象的 nativePath 属性。这应该代表文件位置,包括文件名(以及我猜的扩展名)。

    另一种可能性是访问blob.file 属性。然后就可以使用blob.file.getName()来访问文件名了。

    【讨论】:

    • 已经尝试过,但没有成功:( blob 仅包含图像内容。blob.fileblob.nativePath 都是 null。我将使用我的 blob 的字符串表示形式编辑我的问题。
    • 您的 blob 似乎不代表文件。根据这个问题:stackoverflow.com/questions/13488176/…nativePath 和 file are cannot be used by dev (至少对于 iOS,也许这也是这里的问题。) mimeType = application/octet-stream 表示 blob 不知道哪个文件原始文件的扩展名。
    猜你喜欢
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多