Copy from Camra app:

import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import android.content.ContentResolver; import android.content.ContentValues; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.location.Location; import android.media.ExifInterface; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore.Images; import android.util.Log; public class ImageManager { private static final String TAG = "ImageManager"; private static final Uri STORAGE_URI = Images.Media.EXTERNAL_CONTENT_URI; public static final String CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera"; private ImageManager() { } private static boolean checkFsWritable() { // Create a temporary file to see whether a volume is really writeable. // It's important not to put it in the root directory which may have a // limit on the number of files. String directoryName = Environment.getExternalStorageDirectory().toString() + "/DCIM"; File directory = new File(directoryName); if (!directory.isDirectory()) { if (!directory.mkdirs()) { return false; } } return directory.canWrite(); } public static boolean hasStorage() { return hasStorage(true); } public static boolean hasStorage(boolean requireWriteAccess) { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { if (requireWriteAccess) { boolean writable = checkFsWritable(); return writable; } else { return true; } } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { return true; } return false; } public static int getExifOrientation(String filepath) { int degree = 0; ExifInterface exif = null; try { exif = new ExifInterface(filepath); } catch (IOException ex) { Log.e(TAG, "cannot read exif", ex); } if (exif != null) { int orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, -1); if (orientation != -1) { // We only recognize a subset of orientation tag values. switch(orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } } return degree; } // // Stores a bitmap or a jpeg byte array to a file (using the specified // directory and filename). Also add an entry to the media store for // this picture. The title, dateTaken, location are attributes for the // picture. The degree is a one element array which returns the orientation // of the picture. // public static Uri addImage(ContentResolver cr, String title, long dateTaken, Location location, String directory, String filename, Bitmap source, byte[] jpegData, int[] degree) { // We should store image data earlier than insert it to ContentProvider, // otherwise we may not be able to generate thumbnail in time. OutputStream outputStream = null; String filePath = directory + "/" + filename; try { File dir = new File(directory); if (!dir.exists()) dir.mkdirs(); File file = new File(directory, filename); outputStream = new FileOutputStream(file); if (source != null) { source.compress(CompressFormat.JPEG, 100, outputStream); degree[0] = 0; } else { outputStream.write(jpegData); degree[0] = getExifOrientation(filePath); } } catch (FileNotFoundException ex) { Log.w(TAG, ex); return null; } catch (IOException ex) { Log.w(TAG, ex); return null; } finally { Utils.closeSilently(outputStream); } // Read back the compressed file size. long size = new File(directory, filename).length(); ContentValues values = new ContentValues(9); values.put(Images.Media.TITLE, title); // That filename is what will be handed to Gmail when a user shares a // photo. Gmail gets the name of the picture attachment from the // "DISPLAY_NAME" field. values.put(Images.Media.DISPLAY_NAME, filename); values.put(Images.Media.DATE_TAKEN, dateTaken); values.put(Images.Media.MIME_TYPE, "image/jpeg"); values.put(Images.Media.ORIENTATION, degree[0]); values.put(Images.Media.DATA, filePath); values.put(Images.Media.SIZE, size); if (location != null) { values.put(Images.Media.LATITUDE, location.getLatitude()); values.put(Images.Media.LONGITUDE, location.getLongitude()); } return cr.insert(STORAGE_URI, values); } }

使用方法,如保存指定View到图片文件中:

MainView mMainView = (MainView) findViewById(R.id.main_back); if (false == mMainView.isDrawingCacheEnabled()) { mMainView.setDrawingCacheEnabled(true); } Bitmap bitmap = mMainView.getDrawingCache(); // storeImageToFile(bitmap); storeImage(bitmap, null); mMainView.setDrawingCacheEnabled(false); Toast.makeText(mContext, "Save photo: " + mLastContentUri, Toast.LENGTH_SHORT).show();

更多相关文章

  1. android保存文件
  2. android下保存用户账号密码和读取账号密码
  3. Android(安卓)使用Preferences保存List
  4. android make-standalone-toolchain.sh 使用说明
  5. Android中一直走马灯效果的TextView
  6. Android(安卓)实现SHA1加密算法代码
  7. Android布局案例之人人android九宫格
  8. android布局基础及范例:人人android九宫格布局
  9. Android(安卓)文件保存getFilesDir()丶getCacheDir()、getExtern

随机推荐

  1. 【前端词典】4 个实用有趣的 JS 特性
  2. 3组24个CSS3分享图标,可旋转和悬停
  3. Android振动系统学习
  4. 【前端词典】CDN 带来这些性能优化(进阶必
  5. HTML5 金色漩涡动画
  6. HTML5 3D爱心表白动画
  7. 【前端词典】Vue 使用时常见的 5 个问题
  8. 最全面的CSS3鼠标滑过动画按钮集锦
  9. 【前端词典】输入 URL 到展现涉及的缓存
  10. AngularJS 日期时间选择组件(附详细使用方