How to correctly create new File using the correct path?

0

Trying to create a "FILE" in Android Studio with a path always responds " false " when using the method myfile.exists ().

How can I get the path or Uri to create it correctly? since I add it in different ways but apparently it always turns out to be incorrect.

Examples:

File myfile = new File ( myuri.toString () );

File myfile = new File ("android.resource: //myapps.me.test/drawable/pikachu");

With the file format File myfile = new File ("android.resource: //myapps.me.test/drawable/pikachu.png");

Without "drawable" File myfile = new File ("android.resource: //myapps.me.test/pikachu.png");

I appreciate your support in advance.

    
asked by Tuite 03.03.2018 в 05:54
source

2 answers

0

You can create files in Android in almost the same way you do in any other application written in Java by passing the route, but you have to take into account the place where you want to create it.

To create a file in the external storage of the device, you have to request the permissions in the manifest in the following way:

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

And get the path of the external folder with the function getExternalFilesDir()

On the contrary, if you want to create the file in the internal storage, privately (only the application itself can access it) you do not need to request permissions and you can use the function getFilesDir() to obtain the path of the Private folder of the app.

In this answer you can see an example.

    
answered by 03.03.2018 в 06:30
0

First it is important to add permission

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

but for devices greater than 6.0 or greater this request must be manual

Error showing the external file directory in an AlertDialog in android 6.0 (READ_EXTERNAL_STORAGE / WRITE_EXTERNAL_STORAGE)

use this method:

private void checkExternalStoragePermission() {
    int permissionCheck = ContextCompat.checkSelfPermission(
            this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        Log.i("Mensaje", "No se tiene permiso para leer.");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);
    } else {
        Log.i("Mensaje", "Se tiene permiso para leer!");
    }
}

Now it is important to know that you can not create a file in the resources since they are read only, therefore this is incorrect.

File myfile= new File("android.resource://myapps.me.test/pikachu.png");

Unable to create a file on this route.

    
answered by 05.03.2018 в 20:49