當前位置:編程學習大全網 - 源碼下載 - android 如何將drawable中的圖片保存到系統相冊中

android 如何將drawable中的圖片保存到系統相冊中

android 將drawable中的圖片保存到系統相冊中的原理比較簡單,獲取到的bitmap,然後通過的compress方法寫到壹個fileoutputstream中. 再通知MediaScannerService有圖片文件加入就可以了.

保存圖片的核心代碼如下:

Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.icon);

MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, name, "");

或者

FileOutputStream fos = openFileOutput("image", Context.MODE_PRIVATE);

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

fos.flush();

fos.close();

//發送系統通知消息

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

另壹種方法是直接使用文件流讀寫:

InputStream is = mContext.getResources().openRawResource(PicID);

FileOutputStream fos = new FileOutputStream(LogoFilePath);

byte[] buffer = new byte[8192];

int count = 0;

while((count=is.read(buffer)) > 0)

{

fos.write(buffer, 0, count);

}

fos.close();

is.close();

這裏要註意目錄權限問題:在應用程序AndroidManifest.xml中的manifest節點中加入android:sharedUerId="android.uid.system"這個屬性。然後放在源碼環境中編譯,並通過adb install 的方式進行安裝。mk文件中的屬性改為LOCAL_CERTIFICATE :=platform。

  • 上一篇:四年級讀書作文
  • 下一篇:技術員應用程序源代碼
  • copyright 2024編程學習大全網