Почему не удается поделиться изображением? Изображение перезагружается и при втором нажатии оно успешно - PullRequest
0 голосов
/ 25 июня 2019

При первом нажатии кнопки «Поделиться» изображение перезагружается, а после этого при нажатии кнопки «Поделиться» обмен выполняется успешно.

Я попытался получить позицию 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, и я загружаю изображения в Picasso, используя Url. При первом щелчке обмен не был успешным, но при втором щелчке.

...