0

Не сохраняется файл во внутреннюю память, перепробовал много способов, не сохраняет и не отображает в галерее

private boolean saveBitMap(View drawView) {
    File pictureFileDir;

 /*   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        pictureFileDir = new File(Environment.DIRECTORY_PICTURES + "/" + "Отчеты CheckFusion");
        forToast = "SD/Отчеты CheckFusion/";
    } else {*/
        //pictureFileDir = new File(Environment.getExternalStorageDirectory()+ "/" + "Отчеты CheckFusion");
    //pictureFileDir = new File(Environment.getExternalStorageDirectory(Environment.DIRECTORY_PICTURES)+ "/" + "Отчеты CheckFusion");
    /*pictureFileDir = new File(Environment.getDownloadCacheDirectory()+ "/" + "Отчеты CheckFusion");
        //forToast = "Внутренний накопитель/Отчеты CheckFusion/";
   *//* }*//*
    if (!pictureFileDir.exists()) {
        pictureFileDir.mkdirs();
    }*/
    Date dateNow = new Date();
    @SuppressLint("SimpleDateFormat") SimpleDateFormat formatForDateNow = new SimpleDateFormat("HH:mm dd.MM.yyyy");

    /*String filename = pictureFileDir.getPath() + "/Отчет CheckFusion " + formatForDateNow.format(dateNow) + ".jpg";
    pictureFile = new File(filename);*/
    //pictureFile= new File(Environment.getExternalStorageDirectory(), "Отчет CheckFusion " + formatForDateNow.format(dateNow) + ".jpg");
    pictureFile= new File(this.getExternalFilesDir(null), "Отчет CheckFusion " + formatForDateNow.format(dateNow) + ".jpg");
    Bitmap bitmap = getBitmapFromView(drawView);
    try {
        pictureFile.createNewFile();
        FileOutputStream oStream = new FileOutputStream(pictureFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, oStream);
        oStream.flush();
        oStream.close();
        galleryAddPic(pictureFile);
        /*     openImage(pictureFile);*/
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}
  private void galleryAddPic(File picture) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(picture.getAbsolutePath());
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

Манифест

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
6
  • В манифесте разрешение на доступ прописано? <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    – DrMcSheen
    18 мая 2022 в 7:25
  • Да в манифесте есть, но это не решает проблемы
    – Sergey
    18 мая 2022 в 7:53
  • Что вы хотите сохранить? Рисунок? 18 мая 2022 в 9:30
  • да, рисунок хочу сохранить
    – Sergey
    18 мая 2022 в 9:43
  • bitmap на ноль проверьте, тест записи с простым именем типа test.jpg попробуйте
    – Style-7
    18 мая 2022 в 10:11

1 ответ 1

0

Выяснил, что на 11 андроидах и выше недопустим в названиях :, поэтому происходила ошибка.

    @SuppressLint("SimpleDateFormat") SimpleDateFormat formatForDateNow = new SimpleDateFormat("HH**:**mm dd.MM.yyyy");

Ваш ответ

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

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