1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to store image captured from camera to internal storage of our phone.

Discussion in 'Android Development' started by nagamothu, May 22, 2019.

  1. nagamothu

    nagamothu Newbie
    Thread Starter

    Hi Can you please tell me how to save image in phone internal storage. means if the phone has no SDcard how to store the image?
    this is my code but image is saving in external storage.
    Code (Text):
    1.  
    2. ContextWrapper cw= new ContextWrapper( getApplicationContext() );
    3.         File directory = cw.getDir( "imgDir",Context.MODE_APPEND );
    4.         File destination= new File( directory,"capture_01.bmp" );
    5.        
    6.         ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    7.         image.compress( Bitmap.CompressFormat.JPEG, 100, bytes );
    8.         long filesizeinBytes = image.getByteCount();
    9.         long fileSizeInKB = filesizeinBytes / 1024;
    10.  
    11.         FileOutputStream fo;
    12.         try {
    13.             fo = new FileOutputStream( destination );
    14.             fo.write( bytes.toByteArray() );
    15.             fo.close();
    16.         } catch (IOException e) {
    17.             e.printStackTrace();
    18.             //TODO REMOVE TOASTMESSAGE
    19.             Toast.makeText( this,e.getMessage(),Toast.LENGTH_SHORT ).show();
    20.         }
    21.         Toast.makeText( this,"Image saved sucessfully" ,Toast.LENGTH_SHORT ).show();
    22.     }
    23.  
    Also Tried this but getting error Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference

    Code (Text):
    1.  
    2.  ContextWrapper cw= new ContextWrapper( getApplicationContext() );
    3.         File directory = cw.getDir( "imgDir", Context.MODE_APPEND );
    4.         File destination= new File( directory,"capture_01.bmp" );
    5.        
    6.  
    7.         try {
    8.             MediaStore.Images.Media.insertImage( context.getContentResolver(),destination.getAbsolutePath(),destination.getName(),null );
    9.             context.sendBroadcast( new Intent(
    10.                     Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile( destination )
    11.             ) );
    12.         } catch (FileNotFoundException e) {
    13.             e.printStackTrace();
    14.         }
    15.  
    16.         ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    17.         image.compress( Bitmap.CompressFormat.JPEG, 100, bytes );
    18.         long filesizeinBytes = image.getByteCount();
    19.         long fileSizeInKB = filesizeinBytes / 1024;
    20.  
    21.         FileOutputStream fo;
    22.         try {
    23.             fo = new FileOutputStream( destination );
    24.             fo.write( bytes.toByteArray() );
    25.             fo.close();
    26.         } catch (IOException e) {
    27.             e.printStackTrace();
    28.          
    29.             Toast.makeText( this,e.getMessage(),Toast.LENGTH_SHORT ).show();
    30.         }
    31.  
    32.         Toast.makeText( this,"Image saved sucessfully" ,Toast.LENGTH_SHORT ).show();
    33.     }
    34.  
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - store image captured
  1. alex_laco
    Replies:
    3
    Views:
    189
  2. startac4
    Replies:
    10
    Views:
    599
  3. PascalAuger
    Replies:
    16
    Views:
    1,134
  4. melbourn5
    Replies:
    2
    Views:
    694
  5. Aby PAul
    Replies:
    0
    Views:
    825
  6. GSMenace
    Replies:
    4
    Views:
    1,018
  7. Anmol hira
    Replies:
    3
    Views:
    899
  8. connorhawke
    Replies:
    3
    Views:
    473
  9. Product Manager Adam
    Replies:
    2
    Views:
    963
  10. DonB
    Replies:
    7
    Views:
    1,146

Share This Page

Loading...