Android: Saving image to storage

£可爱£侵袭症+ 提交于 2019-12-07 18:13:29

Step #1: Hold onto the Bitmap somewhere

Step #2: When the button is clicked, fork a background thread, AsyncTask, IntentService, etc. to do the work

Step #3: In the background thread (or whatever), call compress() on the Bitmap, providing an OutputStream on whatever file you want to write to

use Bitmap compress method to store bitmap into the android data storage. and if you want to store full size of image then please use intent.putExtra with file uri or even you can create your own content Provider (Which is my fav).

bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);

http://developer.android.com/training/camera/photobasics.html#TaskPath

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));

Try this:

String filename = "somename.jpg"; // your filename with path to sdcard
File file = new File(filename);
OutputStream outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream); // your bitmap
outStream.flush();
outStream.close();

Using the code you can save image on sd card :

            FileOutputStream outStream = null;
            File f=new File(Environment.getExternalStorageDirectory()+"/My Image/");
            f.mkdir();
            String extStorageDirectory = f.toString();
            File file = new File(extStorageDirectory, "image.jpg");
            pathOfImage = file.getAbsolutePath();
            try {
                outStream = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                Toast.makeText(getApplicationContext(), "Saved at "+f.getAbsolutePath(), Toast.LENGTH_LONG).show();
            } catch (FileNotFoundException e) {e.printStackTrace();}
            try {
                outStream.flush();
                outStream.close();
            } catch (IOException e) {e.printStackTrace();}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!