简体   繁体   English

Titanium android,收到意图后,如何访问本机文件?

[英]Titanium android, receiving intent, how to access to the native file?

In my TiApp.xml i got the following block : 在我的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>

And it's working great. 而且效果很好。 I can go in the photo gallery, select a photo and i can share the photo with my application. 我可以进入照片库,选择一张照片,然后可以与我的应用程序共享照片。 But in my application i got a problem. 但是在我的应用程序中我遇到了一个问题。 I need to access to the filename or at least its extension. 我需要访问文件名或至少扩展名。 But i have only a blob with the content of the image. 但是我只有一个斑点与图像的内容。

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 ?
    }
}

How can i access the native file or its filename ? 如何访问本机文件或其文件名? Or at least its extension ? 或至少是它的扩展名? (that's the only thing i need since i have the file content. I can create a temporary file with the content by i need the extension) (这是我唯一需要的,因为我具有文件内容。我可以通过扩展名来创建包含该内容的临时文件)

In the documentation i can only find examples with simple text files or just displaying the image content. 在文档中,我只能找到带有简单文本文件或仅显示图像内容的示例。 They never access to the native file or its filename. 他们从不访问本地文件或其文件名。

Here the string representation of intent and blob : 这是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>"
}

You should try to read the nativePath property of the blob object. 您应该尝试读取blob对象的nativePath属性。 This should represent the file location including the name of the file (and the extension I guess). 这应该代表文件位置,包括文件名(以及我猜为扩展名)。

Another possibility would be to access the blob.file property. 另一种可能性是访问blob.file属性。 Then you can use blob.file.getName() to access the name of the file. 然后,您可以使用blob.file.getName()来访问文件的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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