Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. public static File getDir() {
  2. File sdDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  3. return new File(sdDir, "MyPics");
  4. }
  5.  
  6. public static boolean writeFile(byte[] data, String fileName, Context context) {
  7. File pictureFileDir = getDir();
  8.  
  9. if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
  10. Toast.makeText(context, "CANT CREATE", Toast.LENGTH_SHORT).show();
  11. }
  12.  
  13. String photoFile = fileName + ".jpg";
  14. String fullPath = pictureFileDir.getAbsolutePath() + File.separator + photoFile;
  15. File pictureFile = new File(fullPath);
  16.  
  17. Toast.makeText(context, "FULL PATH: "+fullPath, Toast.LENGTH_SHORT).show(); // This returns "/storage/emulated/0/Pictures/MyPics/FILENAME.jpg"
  18.  
  19.  
  20. try {
  21. FileOutputStream fos = new FileOutputStream(pictureFile);
  22. fos.write(data);
  23. fos.close();
  24. } catch (Exception error) {
  25. Toast.makeText(context, "File "+fullPath+ "not saved", Toast.LENGTH_SHORT).show();
  26. return false;
  27. }
  28.  
  29. return true;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement