保存图片的代码

来源:互联网 发布:method(i,2))用法Java 编辑:程序博客网 时间:2024/04/30 11:47
Copy from Camra app:

 

[c-sharp] view plaincopy
  1. import java.io.File;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.OutputStream;  
  6. import android.content.ContentResolver;  
  7. import android.content.ContentValues;  
  8. import android.graphics.Bitmap;  
  9. import android.graphics.Bitmap.CompressFormat;  
  10. import android.location.Location;  
  11. import android.media.ExifInterface;  
  12. import android.net.Uri;  
  13. import android.os.Environment;  
  14. import android.provider.MediaStore.Images;  
  15. import android.util.Log;  
  16. public class ImageManager {  
  17.     private static final String TAG = "ImageManager";  
  18.     private static final Uri STORAGE_URI = Images.Media.EXTERNAL_CONTENT_URI;  
  19.       
  20.     public static final String CAMERA_IMAGE_BUCKET_NAME =  
  21.         Environment.getExternalStorageDirectory().toString()  
  22.         + "/DCIM/Camera";  
  23.     private ImageManager() {  
  24.     }  
  25.       
  26.       
  27.       
  28.     private static boolean checkFsWritable() {  
  29.         // Create a temporary file to see whether a volume is really writeable.  
  30.         // It's important not to put it in the root directory which may have a  
  31.         // limit on the number of files.  
  32.         String directoryName =  
  33.                 Environment.getExternalStorageDirectory().toString() + "/DCIM";  
  34.         File directory = new File(directoryName);  
  35.         if (!directory.isDirectory()) {  
  36.             if (!directory.mkdirs()) {  
  37.                 return false;  
  38.             }  
  39.         }  
  40.         return directory.canWrite();  
  41.     }  
  42.     public static boolean hasStorage() {  
  43.         return hasStorage(true);  
  44.     }  
  45.     public static boolean hasStorage(boolean requireWriteAccess) {  
  46.         String state = Environment.getExternalStorageState();  
  47.         if (Environment.MEDIA_MOUNTED.equals(state)) {  
  48.             if (requireWriteAccess) {  
  49.                 boolean writable = checkFsWritable();  
  50.                 return writable;  
  51.             } else {  
  52.                 return true;  
  53.             }  
  54.         } else if (!requireWriteAccess  
  55.                 && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {  
  56.             return true;  
  57.         }  
  58.         return false;  
  59.     }  
  60.       
  61.     public static int getExifOrientation(String filepath) {  
  62.         int degree = 0;  
  63.         ExifInterface exif = null;  
  64.         try {  
  65.             exif = new ExifInterface(filepath);  
  66.         } catch (IOException ex) {  
  67.             Log.e(TAG, "cannot read exif", ex);  
  68.         }  
  69.         if (exif != null) {  
  70.             int orientation = exif.getAttributeInt(  
  71.                 ExifInterface.TAG_ORIENTATION, -1);  
  72.             if (orientation != -1) {  
  73.                 // We only recognize a subset of orientation tag values.  
  74.                 switch(orientation) {  
  75.                     case ExifInterface.ORIENTATION_ROTATE_90:  
  76.                         degree = 90;  
  77.                         break;  
  78.                     case ExifInterface.ORIENTATION_ROTATE_180:  
  79.                         degree = 180;  
  80.                         break;  
  81.                     case ExifInterface.ORIENTATION_ROTATE_270:  
  82.                         degree = 270;  
  83.                         break;  
  84.                 }  
  85.             }  
  86.         }  
  87.         return degree;  
  88.     }  
  89.       
  90.     //  
  91.     // Stores a bitmap or a jpeg byte array to a file (using the specified  
  92.     // directory and filename). Also add an entry to the media store for  
  93.     // this picture. The title, dateTaken, location are attributes for the  
  94.     // picture. The degree is a one element array which returns the orientation  
  95.     // of the picture.  
  96.     //  
  97.     public static Uri addImage(ContentResolver cr, String title, long dateTaken,  
  98.             Location location, String directory, String filename,  
  99.             Bitmap source, byte[] jpegData, int[] degree) {  
  100.         // We should store image data earlier than insert it to ContentProvider,  
  101.         // otherwise we may not be able to generate thumbnail in time.  
  102.         OutputStream outputStream = null;  
  103.         String filePath = directory + "/" + filename;  
  104.         try {  
  105.             File dir = new File(directory);  
  106.             if (!dir.exists()) dir.mkdirs();  
  107.             File file = new File(directory, filename);  
  108.             outputStream = new FileOutputStream(file);  
  109.             if (source != null) {  
  110.                 source.compress(CompressFormat.JPEG, 100, outputStream);  
  111.                 degree[0] = 0;  
  112.             } else {  
  113.                 outputStream.write(jpegData);  
  114.                 degree[0] = getExifOrientation(filePath);  
  115.             }  
  116.         } catch (FileNotFoundException ex) {  
  117.             Log.w(TAG, ex);  
  118.             return null;  
  119.         } catch (IOException ex) {  
  120.             Log.w(TAG, ex);  
  121.             return null;  
  122.         } finally {  
  123.             Utils.closeSilently(outputStream);  
  124.         }  
  125.         // Read back the compressed file size.  
  126.         long size = new File(directory, filename).length();  
  127.         ContentValues values = new ContentValues(9);  
  128.         values.put(Images.Media.TITLE, title);  
  129.         // That filename is what will be handed to Gmail when a user shares a  
  130.         // photo. Gmail gets the name of the picture attachment from the  
  131.         // "DISPLAY_NAME" field.  
  132.         values.put(Images.Media.DISPLAY_NAME, filename);  
  133.         values.put(Images.Media.DATE_TAKEN, dateTaken);  
  134.         values.put(Images.Media.MIME_TYPE, "image/jpeg");  
  135.         values.put(Images.Media.ORIENTATION, degree[0]);  
  136.         values.put(Images.Media.DATA, filePath);  
  137.         values.put(Images.Media.SIZE, size);  
  138.         if (location != null) {  
  139.             values.put(Images.Media.LATITUDE, location.getLatitude());  
  140.             values.put(Images.Media.LONGITUDE, location.getLongitude());  
  141.         }  
  142.         return cr.insert(STORAGE_URI, values);  
  143.     }  
  144. }  
 

 

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

[c-sharp] view plaincopy
  1. MainView mMainView = (MainView) findViewById(R.id.main_back);     
  2.           
  3.         if (false == mMainView.isDrawingCacheEnabled()) {  
  4.             mMainView.setDrawingCacheEnabled(true);  
  5.         }  
  6.           
  7.         Bitmap bitmap = mMainView.getDrawingCache();  
  8. //      storeImageToFile(bitmap);  
  9.         storeImage(bitmap, null);  
  10.         mMainView.setDrawingCacheEnabled(false);  
  11.           
  12.         Toast.makeText(mContext, "Save photo: " + mLastContentUri, Toast.LENGTH_SHORT).show();  
 

来自为知笔记(Wiz)


0 0
原创粉丝点击