【问题标题】:Share intent takes long time to appear分享意图需要很长时间才能出现
【发布时间】:2017-09-07 08:01:06
【问题描述】:

我正在尝试在我的应用程序中包含图像共享,一切正常,但共享选择器需要很长时间才能显示在设备上

这是我正在做的事情:

我有 ImageView "items_details_image",我想分享它的图像以便能够通过 whatsapp 和其他应用程序发送它

void ShareImg() {
    try {
        Uri bmpUri = getLocalBitmapUri(items_details_image);
        if (bmpUri != null) {
            // Construct a ShareIntent with link to image
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
            shareIntent.setType("image/*");
            // Launch sharing dialog for image
            startActivity(Intent.createChooser(shareIntent, "Share Image"));
        } else {
            // ...sharing failed, handle error
        }
    } catch (Exception e) {

    }
}

这是我从图像 URL 获取位图的方法

public Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;
    if (drawable instanceof BitmapDrawable){
        bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
        return null;
    }
    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file =  new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}

我不知道为什么将它与同一设备上的其他应用(例如图库或其他应用)进行比较需要比平时更长的时间

【问题讨论】:

    标签: java android android-intent start-activity android-intent-chooser


    【解决方案1】:

    您正在主应用程序线程上执行磁盘 I/O,在 getLocalBitmapUri() 中。只要将位图写入磁盘,这就会冻结您的 UI。

    【讨论】:

    • 那么我应该做一个异步任务来维护 getLocalBitmapUri 吗?
    • @YazanAllahham:做点别的更好。这个ImageView 从某个地方得到了它的图像。如果该“某处”已经存在于Uri-reachable 位置,则只需使用图像的原始来源。
    • 哇!那太棒了!谢谢你。因为我使用的是 UniversalImageLoader,所以我可以在 ImageLoader 的 onLoadingComplete 事件中使用 Image Uri! ...你节省了我的时间!!!
    猜你喜欢
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 2013-10-11
    • 2012-12-03
    • 2014-07-21
    • 2019-10-26
    相关资源
    最近更新 更多