Search code examples
androidstorageinternal

Android, to save into internal Storage


I'm testing on my divice, I get FALSE in this line

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

So, I don't have external storage in my device, correct?, how can i save a image on my internal storage? if I put this line...

FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);

The class complete

public class ImageStorage {

public static String saveFile(Bitmap bitmap, String filename) {




    try {
        FileOutputStream out = context.openFileOutput(filename, Context.MODE_PRIVATE);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

   }


public static File getImage(String imagename) {

    File mediaImage = null;
    try {
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root);
        if (!myDir.exists())
            return null;

        mediaImage = new File(myDir.getPath() + "/images/"+imagename);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return mediaImage;
}

public static boolean checkifImageExists(String imagename)
{
    Bitmap b = null ;
    File file = ImageStorage.getImage("/"+imagename+".jpg");
    String path = file.getAbsolutePath();

    if (path != null)
        b = BitmapFactory.decodeFile(path); 

    if(b == null ||  b.equals(""))
    {
        return false ;
    }
    return true ;
}

}

The word "context" it's marked as "cannot be resolved". I have spent one week with this issue, please help me


Solution

  • Because this class does not by default has access to a Context, you will need to pass it in the method like this (see how I added the Context parameter):

    public static String saveFile(Context context, Bitmap bitmap, String filename) {}
    

    So when you call it, you will need to pass some kind of context. If you call it from an Activity, you can simply use this.