简体   繁体   English

分享意图需要很长时间才能出现

[英]Share intent takes long time to appear

I'm trying to include image sharing in my application and everything is working but the share chooser takes long time to appear on devices 我正在尝试在我的应用程序中包含图像共享,一切正常,但共享选择器需要很长时间才能出现在设备上

here is what i'm doing: 这是我正在做的事情:

I have ImageView "items_details_image" and I want to share its image to be able to send it through whatsapp and other apps 我有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) {

    }
}

here is how I get bitmap from Image URL 这是我如何从图像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;
}

I don't know why it is taking longer than usual comparing it with other apps on the same device such as gallery or whatever 我不知道为什么花费比平常更长的时间将它与同一设备上的其他应用程序进行比较,例如画廊或其他什么

You are doing disk I/O on the main application thread, in getLocalBitmapUri() . 您正在主应用程序线程上的getLocalBitmapUri()中执行磁盘I / O. This will freeze your UI as long as it takes to write your bitmap to disk. 只要将位图写入磁盘,这将冻结您的UI。

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

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