为什么共享映像失败?图像重新加载,第二次单击成功

时间:2019-06-25 06:17:28

标签: java android android-intent picasso

第一次单击共享按钮时,图像重新加载,然后单击共享按钮,共享成功。

我尝试使用界面获取recyclerView位置,问题仍然存在。

public void WhatsappImage(String url){
    ImageView imageView = findViewById(R.id.image_link);
    Picasso.with(this)
        .load(url)
        .into(imageView);
    Uri bmpUri = getBitmapUri();
    if (bmpUri != null) {
        // Construct a ShareIntent with link to image
        Intent whatsappIntent = new Intent();
        whatsappIntent.setAction(Intent.ACTION_SEND);
        whatsappIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
        whatsappIntent.setPackage("com.whatsapp");
        whatsappIntent.setType("image/*");
        // Launch sharing dialog for image
        startActivity(Intent.createChooser(whatsappIntent, "Share Image"));
    } else {
        Toast.makeText(this, R.string.whats_sharing_failed,Toast.LENGTH_SHORT).show();
    }
}

public void ShareImage(String url){
    imageView = findViewById(R.id.image_link);
    Picasso.with(this)
        .load(url)
        .into(imageView);
    bmpUri = getBitmapUri();
    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 {
        Toast.makeText(this, R.string.sharing_failed,Toast.LENGTH_SHORT).show();
    }
}

public Uri getBitmapUri() {
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp;
    if (drawable instanceof BitmapDrawable) {
        bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
        return null;
    }
    bmpUri = null;
    try{
        File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),"Share Image" + System.currentTimeMillis() + ".png");
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG,80,out);
        out.close();
        bmpUri = FileProvider.getUriForFile(SecondaryActivity.this,"com.hindiJoke.fileprovider", file);
    }catch (IOException e){
        e.printStackTrace();
    }
    return bmpUri;
}

我已经将图像保存在Firebase Storage上,并且正在使用Url在Picasso中加载图像。第一次点击共享失败,但是第二次点击共享。

0 个答案:

没有答案