0
 public void onClick(View view) {
        takeScreenshot();
    }

    private void takeScreenshot() {
        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

        try {
            // image naming and path  to include sd card  appending name you choose for file
            String mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + now + ".jpg";

            // create bitmap screen capture
            View v1 = getWindow().getDecorView().getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);

            File imageFile = new File(mPath);

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();

            openScreenshot(imageFile);
        } catch (Throwable e) {
            // Several error may come out with file handling or DOM
            e.printStackTrace();
        }
    }

    private void openScreenshot(File imageFile) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(imageFile);
        intent.setDataAndType(uri, "image/*");
        startActivity(intent);
    }
}

1 ответ 1

0

Попробуйте так:

String folder_main = "YourFolder";

File f = new File(Environment.getExternalStorageDirectory(), folder_main);
if (!f.exists()) {
    f.mkdirs();
}

но не забудьте добавить в AndroidManifest.xml такую строку:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

надеюсь поможет.

3
  • В общем папку создаёт но сохраняет как и раньше в DOWNLOADS 11 фев 2019 в 18:04
  • так вам нужно дальше вместо папки загрузок указывать свежесозданную папку))) вот вы создали папку дальше вы ее находите так же как и папку загрузок и туда уже сохраняете ваш скриншот
    – Andrew
    11 фев 2019 в 18:09
  • 1
    String mPath = Environment.getExternalStoragePublicDirectory(folder_main) + "/" + now + ".jpg"; Работает CПАСИБО 11 фев 2019 в 18:38

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.