保存圖片的核心代碼如下:
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。