The result of "File.mkdirs ()" is ignored

This is my code inside myDir.mkdirs();, this code shows me that the result warning is File.mkdirs()ignored.

I am trying to fix this warning, but I have failed.

   private void saveGIF() {
            Toast.makeText(getApplicationContext(), "Gif Save", Toast.LENGTH_LONG).show();
            String filepath123 = BuildConfig.VERSION_NAME;
            try {
                File myDir = new File(String.valueOf(Environment.getExternalStorageDirectory().toString()) + "/" + "NewyearGIF");enter code here

    //My Statement Code This Line Show Me that Warning

 myDir.mkdirs();

                File file = new File(myDir, "NewyearGif_" + System.currentTimeMillis() + ".gif");
                filepath123 = file.getPath();
                InputStream is = getResources().openRawResource(this.ivDrawable);
                BufferedInputStream bis = new BufferedInputStream(is);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] img = new byte[AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT];
                while (true) {
                    int current = bis.read();
                    if (current == -1) {
                        break;
                    }
                    baos.write(current);
                }
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(baos.toByteArray());
                fos.flush();
                fos.close();
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
            mediaScanIntent.setData(Uri.fromFile(new File(filepath123)));
            sendBroadcast(mediaScanIntent);
        }
+6
source share
7 answers

The method mkdirshas a return value booleanthat you have not used.

 boolean wasSuccessful = myDir.mkdirs();

The create operation returns a value that indicates whether directory creation was successful. For example, the result value wasSuccessfulcan be used to display an error when it is false.

if (!wasSuccessful) { 
    System.out.println("was not successful."); 
}

From Java docs about return value boolean:

true , ; false

+12

mkdir , - , .

+3
File myDirectory = new File(Environment.getExternalStorageDirectory(),"NewyearGIF");
if(!myDirectory.exists()) {
   myDirectory.mkdirs(); 
}else{
   // Directory already exist
}
+1

, , , () :

File.mkdir()

... - . , mkdir(), , .

- . , () , , .

(, ...)

, - .

... BAD STYLE Java. Java - ,

   public void savefiles(Context c,Bitmap image,String name)
    {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

        if (!path.exists()) {
              if(path.mkdir()){
                  // do something
              }
        }
    }

. - , , .

0

, , :

File imageThumbsDirectory = getBaseContext.getExternalFilesDir("ThumbTemp");

if(imageThumbsDirectory != null) {
    if (!imageThumbsDirectory.exists()) {
        if (imageThumbsDirectory.mkdir()) ; //directory is created;
    }
}
0
source
File CDir = new File(Environment.getExternalStorageDirectory(), IMPORT_DIRECTORY);
if (!CDir.exists()) {
    boolean mkdir = CDir.mkdir();
    if (!mkdir) {
        Log.e(TAG, "Directory creation failed.");
    }
}

mkdir returns a boolean value. we need to intercept the return value from mkdir. Replace your code with this and the check (warning of Result of File.mkdirs () is ignored.) Will disappear

0
source

Just enter this code:

File myDirectory = new File(Environment.getExternalStorageDirectory(),"NewyearGIF");
if(!myDirectory.exists()) {
   myDirectory.mkdirs(); 
}else{
   // Directory already exist
}

If the application works above Lollipop, you need to add permission to run for storage.

-1
source

Source: https://habr.com/ru/post/1664752/


All Articles