Save bitmap to app folder

前端 未结 5 1322
青春惊慌失措
青春惊慌失措 2020-12-15 02:03

When my app first starts, it lets the user select a profile picture. This can be done taking a photo at the moment, or selecting it from the gallery.

After the user

5条回答
  •  Happy的楠姐
    2020-12-15 02:47

    understand your requirement. Below pasting some piece of code which i tested and working.Basically get the image from camera and save it in app storage.please go through it. Hope this helps.Cheers..

    //For saving image...

    private String saveToInternalSorage(Bitmap bitmapImage) {
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        // Create imageDir
        File mypath = new File(directory, "profile.jpg");
    
        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);
            fos.close();
            Editor editor = sharedpreferences.edit();
            editor.putString("saved", "na");
            editor.commit(); 
    
        } catch (Exception e) {
            e.printStackTrace();
        }
        return directory.getAbsolutePath();
    }
    

    //..To load image from storage

    private void loadImageFromStorage(String path) {
    
        try {
            ContextWrapper cw = new ContextWrapper(getApplicationContext());
            File path1 = cw.getDir("imageDir", Context.MODE_PRIVATE);
            File f = new File(path1, "profile.jpg");
            Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
            ImageView img = (ImageView) findViewById(R.id.viewImage);
            img.setImageBitmap(b);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    
    }
    

提交回复
热议问题