Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android save images to internal storage

Tags:

android

I'm having problems implementing this code Saving and Reading Bitmaps/Images from Internal memory in Android

to save and retrieve the image that I want, here is my code:

ContextWrapper cw = new ContextWrapper(getApplicationContext());
             // path to /data/data/yourapp/app_data/imageDir
            File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
            // Create imageDir
            File mypath=new File(directory, + name + "profile.jpg");

            FileOutputStream fos = null;
            try {           

                fos = new FileOutputStream(mypath);

           // Use the compress method on the BitMap object to write image to the OutputStream
                myBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

and to retrieve(I don't know if I'm doing wrong)

 @Override
   protected void onResume()
   {
      super.onResume();

      try {
          File f  = new File("imageDir/" + rowID, "profile.jpg");
        Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
        image = (ImageView) findViewById(R.id.imageView2);
        image.setImageBitmap(b);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

and nothing happens so what should I change??

like image 807
user2580401 Avatar asked Oct 19 '13 04:10

user2580401


People also ask

How do I save pictures to my internal storage?

Once you tap on the cog-wheel, you'll be in your camera's settings; swipe all the way down until you get to the Save settings sections. There, you'll find the Storage option, tap on it, and your Android device should give you the option to change the storage path.

How do I use internal storage on Android?

All you have to do is open that app and select the "Show internal storage" option in its menu to browse through your phone's full internal storage. You can then open, move, rename, copy, delete, and share files as needed.

What is internal storage on Android?

Android Internal storage is the storage of the private data on the device memory. By default, saving and loading files to the internal storage are private to the application and other applications will not have access to these files.


3 Answers

To Save your bitmap in sdcard use the following code

Store Image

private void storeImage(Bitmap image) {
    File pictureFile = getOutputMediaFile();
    if (pictureFile == null) {
        Log.d(TAG,
                "Error creating media file, check storage permissions: ");// e.getMessage());
        return;
    } 
    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        image.compress(Bitmap.CompressFormat.PNG, 90, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d(TAG, "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, "Error accessing file: " + e.getMessage());
    }  
}

To Get the Path for Image Storage

/** Create a File for saving an image or video */
private  File getOutputMediaFile(){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this. 
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
            + "/Android/data/"
            + getApplicationContext().getPackageName()
            + "/Files"); 

    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            return null;
        }
    } 
    // Create a media file name
    String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
    File mediaFile;
        String mImageName="MI_"+ timeStamp +".jpg";
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);  
    return mediaFile;
} 
like image 79
August Falbo Avatar answered Sep 28 '22 00:09

August Falbo


I think Faibo's answer should be accepted, as the code example is correct, well written and should solve your specific problem, without a hitch.

In case his solution doesn't meet your needs, I want to suggest an alternative approach.

It's very simple to store image data as a blob in a SQLite DB and retrieve as a byte array. Encoding and decoding takes just a few lines of code (for each), works like a charm and is surprisingly efficient.

I'll provide a code example upon request.

Good luck!

like image 21
Brandon Avatar answered Sep 28 '22 02:09

Brandon


Note that you are saving the pick as name + profile.jpg under imageDir directory and you're trying to retrieve as profile.jpg under imageDir/[rowID] directory check that.

like image 24
Jans Avatar answered Sep 28 '22 01:09

Jans