簡體   English   中英

分享意圖需要很長時間才能出現

[英]Share intent takes long time to appear

我正在嘗試在我的應用程序中包含圖像共享,一切正常,但共享選擇器需要很長時間才能出現在設備上

這是我正在做的事情:

我有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;
}

我不知道為什么花費比平常更長的時間將它與同一設備上的其他應用程序進行比較,例如畫廊或其他什么

您正在主應用程序線程上的getLocalBitmapUri()中執行磁盤I / O. 只要將位圖寫入磁盤,這將凍結您的UI。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM