Tuesday 11 September 2012

Android, Create File

How to create Folder on External Memory in Android.

public boolean isExternalStorageWritable() {
  if (Environment.MEDIA_MOUNTED.equals(Environment
                        .getExternalStorageState()))
     return true;
   else
       return false;
   }

File folder;

if (isExternalStorageWritable()) {
      dbFolder = new File(
                    Environment.getExternalStorageDirectory()
                               + File.separator + "Mohammad");                
   }
  boolean success = false;
  if (!dbFolder.exists()) {
      success = dbFolder.mkdir();//one directory
// success = dbFolder.mkdir(); to create more than one directory 
//                        like "Mohammad/kaied/abuhmead"
  }
  if (!success) {
            DB_DIRE=null;
         }
  

Mohammad Abu Hmead

Tuesday 4 September 2012

Android; Read file from Assets into Byte Array

Sometimes you want to place an encrypted file in the assets folder, and you want to read it in correct and unencrypted, so, you want to read it into byte array and edit the byte array to the correct format, here is the way


InputStream is=getAssets().open("fileName.extension");
byte[] fileBytes=new byte[is.available()];
is.read( fileBytes);
is.close();

Now you edit the byte array as you want, and you can create original file with the current bytes if it was encrypted



Mohammad Abu Hmead