无法获取如何使用来自android选择器意图的传入图像

时间:2019-05-23 10:03:00

标签: nativescript

我正在尝试在主页上显示图像。将图片分享到我的应用后,该图片将来自whats应用的个人资料。

app.js

const marker = L.marker(eaPoint.position, {
    icon: L.icon({
      iconUrl: ICONS.ruby,
      iconSize: [30, 40],
    }),
    riseOnHover: true,
    data: eaPoint.data,
  })

xml

if (intent.getClipData()) {
        let imageUri = intent.getClipData().getItemAt(0).getUri()
        console.log(imageUri);
    //  content://com.whatsapp.fileprovider/external/WhatsApp/.Shared/photo.jpg
        global.imageUri = imageUri;
    }

viewModel

<StackLayout class="p-20">
       <Image src="{{ imageUri }}" stretch="none" />

    </StackLayout>

遇到此错误

imageUri: global.imageUri,

我需要知道如何使用来自android意向服务的传入图像。

2 个答案:

答案 0 :(得分:0)

您不能使用内容uri直接显示图像。您将必须使用MediaStore

解析实际文件的url。

您可以参考nativescript-imagepicker的源代码以获取工作示例。从图库中选择图像后,解析内容uri后,它将返回实际的文件路径。

答案 1 :(得分:0)

在Android清单中添加这些内容

<intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>

要处理它,请将此代码复制粘贴到您喜欢的任何地方

if (platform_1.isAndroid) {
        application.android.on(application.AndroidApplication.activityCreatedEvent, function (args) {
            var activity = args.activity;
            // Get intent, action and MIME type
            var intent = activity.getIntent();
            var action = intent.getAction();
            var type = intent.getType();
            if (android.content.Intent.ACTION_SEND === action && type != null) {
                if (type.startsWith("text/")) {
                    handleSendText(intent); // Handle text being sent
                }
                else if (type.startsWith("image/")) {
                    handleSendImage(intent); // Handle single image being sent
                }
            }
            else if (android.content.Intent.ACTION_SEND_MULTIPLE === action && type != null) {
                if (type.startsWith("image/")) {
                    handleSendMultipleImages(intent); // Handle multiple images being sent
                }
            }
            else {
                // Handle other intents, such as being started from the home screen
            }
        });
    }
function handleSendText(intent) {
    if (platform_1.isAndroid) {
        var sharedText = intent.getStringExtra(android.content.Intent.EXTRA_TEXT);
        if (sharedText != null) {
            // Update UI to reflect text being shared
            console.log("sharedText: ", sharedText);
            console.log("Text received!");
            vm.set("sharedText", sharedText);
        }
    }
}
function handleSendImage(intent) {
    if (platform_1.isAndroid) {
        var imageUri = intent.getParcelableExtra(android.content.Intent.EXTRA_STREAM);
        if (imageUri != null) {
            // Update UI to reflect image being shared
            console.log("Image received!");
            var appContext = application.android.context;
            var bitmap = android.provider.MediaStore.Images.Media.getBitmap(appContext.getContentResolver(), imageUri);
            console.log("bitmap: ", bitmap);
            vm.set("bitmap", bitmap);
        }
    }
}
function handleSendMultipleImages(intent) {
    if (platform_1.isAndroid) {
        var imageUris = intent.getParcelableArrayListExtra(android.content.Intent.EXTRA_STREAM);
        if (imageUris != null) {
            // Update UI to reflect multiple images being shared
            console.log("imageUris: ", imageUris);
            console.log("Multiple images received!");
        }
    }
}