将图片保存到系统相冊的两种方法
生活随笔
收集整理的這篇文章主要介紹了
将图片保存到系统相冊的两种方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一種:採用系統的api直接使用:
ContentResolver cr = getContentResolver();String url = MediaStore.Images.Media.insertImage(cr, bmp,String.valueOf(System.currentTimeMillis()), "");可是,這樣的方式必須得刷新圖庫: sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
雖然如此,這樣的方法還是僅僅能適合安卓4.4下面的手機,若是4.4以上的手機就會報錯。因此建議採用另外一種方式來寫。
另外一種:直接採用文件流進行保存到相冊
File tempFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/"+ String.valueOf(System.currentTimeMillis()) + ".png");if(tempFile.exists()){tempFile.delete();}try {tempFile.createNewFile();} catch (IOException e) {e.printStackTrace();}FileOutputStream fOut = null;try {fOut = new FileOutputStream(tempFile);} catch (FileNotFoundException e) {e.printStackTrace();}bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);try {fOut.flush();fOut.close();} catch (IOException e) {// TODO: handle exceptione.printStackTrace();}最后把整個方法貼出來:
/*** 將ImageView中的圖片保存到系統相冊*/private void SaveImageToSysAlbum() {if (FileUtil.isSdCardExist()) {BitmapDrawable bmpDrawable = (BitmapDrawable)mFullImageView.getDrawable();Bitmap bmp = bmpDrawable.getBitmap();if (bmp != null) {try {/*ContentResolver cr = getContentResolver();String url = MediaStore.Images.Media.insertImage(cr, bmp,String.valueOf(System.currentTimeMillis()), "");*/File tempFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/"+ String.valueOf(System.currentTimeMillis()) + ".png");if(tempFile.exists()){tempFile.delete();}try {tempFile.createNewFile();} catch (IOException e) {e.printStackTrace();}FileOutputStream fOut = null;try {fOut = new FileOutputStream(tempFile);} catch (FileNotFoundException e) {e.printStackTrace();}bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);try {fOut.flush();fOut.close();} catch (IOException e) {// TODO: handle exceptione.printStackTrace();}Toast.makeText(this, getString(R.string.save_succ), Toast.LENGTH_SHORT).show();} catch (Exception e) {e.printStackTrace();}}else {Toast.makeText(this, getString(R.string.no_iamge_save_fail), Toast.LENGTH_SHORT).show();}}else {Toast.makeText(this, getString(R.string.no_sdcard_save_fail), Toast.LENGTH_SHORT).show();}String release = android.os.Build.VERSION.RELEASE;String tempID = release.substring(0, 3);if(Double.parseDouble(tempID) >= 4.4){//安卓4.4以上版本號的時候使用這個。下面的使用else語句里面的MediaScannerConnection.scanFile(this,new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" }, null,null);}else {sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));MediaScannerConnection.scanFile(this,new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" }, null,null); }}總結
以上是生活随笔為你收集整理的将图片保存到系统相冊的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于Core Data的一些整理(一)
- 下一篇: 黄聪:C# 开发Chrome内核浏览器(