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. ViewModel + SavedState
  4. Gradle 属性总结收录
  5. Android在布局文件指定位置动态增加删除布局
  6. Android--截屏(支持Android7.1.1,无需root)
  7. 抓取Android(安卓)& Linux网络包
  8. Android(安卓)将bitmap保存为本地png图片
  9. Toast 使用方法

随机推荐

  1. 盘点Android应用开发中曾经很流行但是已
  2. Android的四种监听事件处理方式
  3. 一切为了更贴近系统―Android中的线程模
  4. Android、Symbian系统手机通讯录和Google
  5. Android框架浅析
  6. 创建一个QT for Android的传感器应用应用
  7. CSS进阶知识flex弹性布局容器与项目
  8. android ICS 4.0.3 tablet模式 插入usb线
  9. Android(安卓)怎样通过蓝牙传输文件
  10. android 数据库处理及操作