Android 中儲存圖片的代碼

來源:互聯網
上載者:User

Copy from Camra app:

 

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

 

使用方法,如儲存指定View到圖片檔案中:

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.