Делаем скриншот и делимся им на Android Studio - PullRequest
0 голосов
/ 08 апреля 2020

Я попытался сделать скриншот моего экрана, сохранить его и поделиться своим сохраненным файлом. Кодекс кажется мне правильным, но он не работает. Может кто-нибудь помочь мне решить эту проблему? publi c void takeScreenshot (Просмотр представления) {

    View view1 = getWindow().getDecorView().getRootView();
    view1.setDrawingCacheEnabled(true);

    Bitmap bitmap = Bitmap.createBitmap(view1.getDrawingCache());
    view1.setDrawingCacheEnabled(false);

    String filepath = Environment.getExternalStorageDirectory() + "/Downloads/" + Calendar.getInstance().getTime().toString() + ".jpg";

    File fileoutput = new File(filepath);

    FileOutputStream fileOutputStream;

    try {
        fileOutputStream = new FileOutputStream(fileoutput);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
        shareScreenshot(fileoutput);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

publi c void shareScreenshot (файл файла) {

        Uri uri = Uri.fromFile(file);
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("image/*");
        String shareBody = "In Tweecher, My highest score with screen shot";
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Tweecher score");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }
...