Поделитесь изображением и текстом с Intent to Whats App - PullRequest
0 голосов
/ 24 января 2019

Я пытаюсь поделиться изображением из imageview с текстовой надписью в whatsApp, но решения, которые я нашел в Интернете, похоже, не работают для меня.

View content = findViewById(R.id.posted_house);
        content.setDrawingCacheEnabled(true);
        Bitmap bitmap = content.getDrawingCache();
        File root = Environment.getExternalStorageDirectory();
        File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
        try
        {
            root.createNewFile();
           FileOutputStream ostream = new FileOutputStream(cachePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
            ostream.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        Intent txtIntent = new Intent(android.content.Intent.ACTION_SEND);
        txtIntent .setType("image/*");
        txtIntent .putExtra("message");
        txtIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
        startActivity(Intent.createChooser(txtIntent ,"Share"));
    }

1 Ответ

0 голосов
/ 24 января 2019
  • Вот код для обмена изображением и текстом в WhatsApp ..

    View screenView = rootView.getRootView();
    screenView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
    screenView.setDrawingCacheEnabled(false);
    
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, "Title", null);
    
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("image/*");
    
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "Your message");
    intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
    try {
        startActivity(Intent.createChooser(intent, "Share Screenshot"));
    } catch (ActivityNotFoundException e) {
        Toast.makeText(this, "No App Available", Toast.LENGTH_SHORT).show();
    }
    
...