Reputation: 1171
I need my App to make a folder on the SD card and in the folder it need to include an image.
How would i go about doing this?
-
Thanks
Upvotes: 0
Views: 144
Reputation: 3047
it will create a folder in your sdcard in the name of MyFoler and the create a new .png file...
new File(Environment.getExternalStorageDirectory().getAbsoluteFile().toString()+"/MyFolder").mkdir();
File file = new File(Environment
.getExternalStorageDirectory().getAbsolutePath()+ "/MyFolder/img_dinash" + ".png");
try{
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush(); out.close();
}
catch (FileNotFoundException fe) {
// TODO: handle exception
}
catch (IOException e) {
// TODO: handle exception
}
Upvotes: 1
Reputation: 1327
in your manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in your code you can use
// create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/[folderName]/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
Upvotes: 2