1

我想包括在我的應用圖像共享和一切工作,但份額選擇器需要很長時間才能出現在設備分享意圖需要較長時間才能顯示

這裏是我在做什麼:

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

我不知道爲什麼它會比平常久其與同一個設備上的其他應用程序,如畫廊或任何比較

回答

1

你是做盤我的主應用程序線程/ O,在getLocalBitmapUri() 。只要將位圖寫入磁盤,就會凍結用戶界面。

+0

所以我應該做一個asynctask來維護getLocalBitmapUri? –

+1

@YazanAllahham:更好的是做別的事情。這個'ImageView'從某個地方得到了它的圖像。如果「某處」已經存在於「Uri」可到達的位置,則只需使用圖像的原始來源。 – CommonsWare

+0

Wooow!太棒了!謝謝。因爲我使用的是UniversalImageLoader,所以我可以在ImageLoader的onLoadingComplete事件中使用Image Uri! ...你救了我幾個小時! –