Check Out Mobile Application Proposal

Save Files | Images | Videos in internal storage or Cache memory

Cache Memory

  • Process is faster then internal memory 
  • you can store very low capacity when compare to internal memory
  • can't make the cache memory too large , it causes to error . (OutOfMemoryExceptio)
  • You can see the images in the gallery. 
  • No one can see or use the image 
  • Only from the your application other application can't access the data
Internal Memory

  • Little bit slower than cache memory
  • you can store high capacity
  • stored images can view in the internal storage or gallery
  • it's public any one can see and use it 

ContextWrapper will help to get the cache memory location in android studio, the you can get the directory in the file , The below function will store the bitmap to cache storage.

public static String saveToCacheMemory(Activity activity, Bitmap bitmapImage){

SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);

ContextWrapper cw = new ContextWrapper(activity);
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,mDateFormat.format(new Date()) + StaticVariables.imageFormat);

Log.d(TAG, "directory: " + directory);

FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
Log.d(TAG, "bit exception: Success" );
} catch (Exception e) {
Log.d(TAG, "bit exception: " + e.getMessage());
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, "io exce: " + e.getMessage());
}
}
Log.d(TAG, "absolute path " + directory.getAbsolutePath());
return mypath.getAbsolutePath();
}

The below function will get the internal storage location from the mobile
/*
this method give you the path to store the image in the internal storage
1. the application directory name
2. file format your going to save ex. .jpg,.mp4,.pdf
3. internal storage public directory type
*/
public static File getInternalStorageDir(String internalStorageDir, String fileType, String dirType){
Date date = new Date();
String milisec = String.valueOf(date.getTime());
File photosDir = new File(Environment.getExternalStoragePublicDirectory(dirType) + internalStorageDir);
// Create imageDir
if (!photosDir.exists()) photosDir.mkdir();
return new File(photosDir, milisec + fileType);
}

You can use this method to store the bitmap in the internal storage 

public static String saveTOInternamMemory(Activity activity, Bitmap bitmapImage){

File myPath = StaticMethods.getInternalStorageDir("images",".mp4",Environment.DIRECTORY_PICTURES);

Log.d(TAG, "directory: " + myPath.getAbsolutePath());

FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
Log.d(TAG, "bit exception: Success" );
} catch (Exception e) {
Log.d(TAG, "bit exception: " + e.getMessage());
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, "io exce: " + e.getMessage());
}
}
Log.d(TAG, "absolute path " + myPath.getAbsolutePath());
return myPath.getAbsolutePath();
}

Then finally don't forget to enable the permission in the  AndroidManifest.xml 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage"/>
I think will help you. 
Thankyou

Comments