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.
Also Tried this but getting error Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
this is my code but image is saving in external storage.
Code:
ContextWrapper cw= new ContextWrapper( getApplicationContext() );
File directory = cw.getDir( "imgDir",Context.MODE_APPEND );
File destination= new File( directory,"capture_01.bmp" );
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress( Bitmap.CompressFormat.JPEG, 100, bytes );
long filesizeinBytes = image.getByteCount();
long fileSizeInKB = filesizeinBytes / 1024;
FileOutputStream fo;
try {
fo = new FileOutputStream( destination );
fo.write( bytes.toByteArray() );
fo.close();
} catch (IOException e) {
e.printStackTrace();
//TODO REMOVE TOASTMESSAGE
Toast.makeText( this,e.getMessage(),Toast.LENGTH_SHORT ).show();
}
Toast.makeText( this,"Image saved sucessfully" ,Toast.LENGTH_SHORT ).show();
}
Also Tried this but getting error Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
Code:
ContextWrapper cw= new ContextWrapper( getApplicationContext() );
File directory = cw.getDir( "imgDir", Context.MODE_APPEND );
File destination= new File( directory,"capture_01.bmp" );
try {
MediaStore.Images.Media.insertImage( context.getContentResolver(),destination.getAbsolutePath(),destination.getName(),null );
context.sendBroadcast( new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile( destination )
) );
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress( Bitmap.CompressFormat.JPEG, 100, bytes );
long filesizeinBytes = image.getByteCount();
long fileSizeInKB = filesizeinBytes / 1024;
FileOutputStream fo;
try {
fo = new FileOutputStream( destination );
fo.write( bytes.toByteArray() );
fo.close();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText( this,e.getMessage(),Toast.LENGTH_SHORT ).show();
}
Toast.makeText( this,"Image saved sucessfully" ,Toast.LENGTH_SHORT ).show();
}