Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Result of 'File.mkdirs()' is ignored

Tags:

java

android

This is my Code inside myDir.mkdirs(); this code show me that warning of Result of File.mkdirs() is ignored.

I try to fix this Warning but I 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);
        }
like image 379
Jaymin Bhadani Avatar asked Dec 24 '16 10:12

Jaymin Bhadani


People also ask

Which of the following best describes the difference between mkdir () and Mkdirs () methods?

mkdirs() will create the specified directory path in its entirety where mkdir() will only create the bottom most directory, failing if it can't find the parent directory of the directory it is trying to create. In other words mkdir() is like mkdir and mkdirs() is like mkdir -p . new File("/tmp/one/two/three").

What is mkdir in Java?

The mkdir() function is used to create a new directory denoted by the abstract pathname. The function returns true if directory is created else returns false. Function Signature: public boolean mkdir() Syntax: file.mkdir()


2 Answers

The method mkdirs has a boolean return value, which you didn't use.

 boolean wasSuccessful = myDir.mkdirs();

The create operation returns a value, which indicates if the creation of the directory was successful. For example, the result value wasSuccessful can be used to display an error when it is false.

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

From the Java docs about the boolean return value:

true if and only if the directory was created, along with all necessary parent directories; false otherwise

like image 181
Kevin Wallis Avatar answered Oct 08 '22 10:10

Kevin Wallis


File CDir = new File(Environment.getExternalStorageDirectory(), IMPORT_DIRECTORY);
if (!CDir.exists()) {
    boolean mkdir = CDir.mkdir();
    if (!mkdir) {
        Log.e(TAG, "Directory creation failed.");
    }
}

mkdir return a Boolean value. we need to catch the return value from mkdir .Replace your code with this and check (warning of Result of File.mkdirs() is ignored.) will be gone

like image 43
riju Avatar answered Oct 08 '22 12:10

riju