简体   繁体   中英

Is it possible to create a new folder?

Is it possible to create a new folder for images (to be taken through my app) to be stored in, on the android phone? (Or SD Card as far as I'm concerned). I could name it what I like [Maybe the name of my app so the files are easily found] and then the images taken by launching the camera through my app will be stored there. I'm thinking it might have something to do with Uri's. (Just a guess.)

use this code

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");   
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

} catch (Exception e) {
       e.printStackTrace();
}

and add this in manifest

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

check this link Android saving file to external storage

Just use File operation for that..

File imageDirectory = new File("/sdcard/Images/"); // Path for location where you want to make directory, Internal or External storage
// have the object build the directory structure, if needed.
imageDirectory.mkdirs();

And in Application's manifest file put permission..

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

Why not?

String path = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/YourAppRootDir"; 
File dir = new File(path);
dir.mkdirs();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM