Android, to save into internal Storage

151 views Asked by At

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

2

There are 2 answers

4
Jeffrey Klardie On BEST ANSWER

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.

3
stealthjong On
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();
    }
}

The reason that your code doesn't work: context cannot be resolved. Within the code, there's nothing called context. You have to add it as parameter (your Activity extends Context, so you can pass that as parameter, and call this in your Activity: saveFile(this, mbitmap, mfilename)

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

That, or you have to remove static, and make the method a member of your Activity class, and change context into this (or remove it). See below.

public class MyActivity extends Activity {
    //your activity members

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

if the method is called from an inner AsyncTask class, call MyActivity.this.openFileOutput(filename, Context.MODE_PRIVATE)