保存图片的代码
来源:互联网 发布:method(i,2))用法Java 编辑:程序博客网 时间:2024/04/30 11:47
Copy from Camra app:
[c-sharp] view plaincopy
- 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到图片文件中:
[c-sharp] view plaincopy
- 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();
来自为知笔记(Wiz)
0 0
- 保存图片的代码
- GMF的保存图片代码
- 关于python保存图片的代码
- 保存 BITmap 图片 代码
- 禁止鼠标右键保存图片、禁止拖动图片的代码
- AspJpeg组件实现远程图片保存的实例完整代码
- 保存图片到磁盘上的js代码
- php上传图片的代码并保存到数据库
- 把google map地图图片保存下来的代码
- php上传图片的代码并保存到数据库
- 用c++语言打开/保存一幅图片的代码
- php上传图片的代码并保存到数据库
- PHP从网站抓取图片并保存本地的代码
- 关于创建涂鸦UI以及保存涂鸦图片的代码
- C++语言读取和保存单张图片的代码
- 图片的保存格式
- 图片的保存
- 图片的保存
- 3.Android loader详解___重启与回调
- 积分图像与积分直方图
- 2.Android loader详解___使用加载器
- 4.Android loader详解___回调
- notification
- 保存图片的代码
- 代码片段--Button抢焦点, 无论点在哪都是button触发. 解决方法很简单, 在item最外层的布局中加一个配置:
- viewholder到底用什么修饰/
- 1.精确区配联系人 11位 or 7位
- 设置字符串光标位置
- 列表长按菜单对应的选项ID
- 监听音量上下键调节音量
- editText中设置回车输入不换行
- 如何设置通知栏下拉不可用