1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
public class ImageManager { private Context mContext; private String fileFullPath; public ImageManager(Context context) { mContext = context; } /** * 画像の保存 * * @param bitmap * @param albumName */ public void save(Bitmap bitmap, String albumName) { if (!canUseSd()) { Log.e("Error", "Can't use SD Card"); return; } saveToSd(getSdStorageDir(albumName), bitmap); } /** * ストレージに画像保存 * * @param dir * @param bitmap */ private void saveToSd(File dir, Bitmap bitmap) { String fileName = getFileName(); fileFullPath = dir.getAbsolutePath() + "/" + fileName; Log.d("保存パス","fileFullPath= " + fileFullPath); try { // 保存処理 FileOutputStream fos = new FileOutputStream(fileFullPath); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (Exception e) { Log.e("Error", "" + e.toString()); } finally { // アルバムに反映 addGallery(fileName); } } /** * 保存した画像をギャラリーに追加 * * @param fileName */ private void addGallery(String fileName) { try { ContentValues values = new ContentValues(); ContentResolver contentResolver = mContext.getContentResolver(); values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); values.put(MediaStore.Images.Media.TITLE, fileName); values.put("_data", fileFullPath); contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } catch (Exception e) { Log.e("Error", "" + e); } } /** * 画像のファイル名を日付から生成し取得 * * @return */ private String getFileName() { Date mDate = new Date(); SimpleDateFormat fileNameFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH); String fileName = fileNameFormat.format(mDate) + ".jpg"; return fileName; } /** * ストレージのストレージパス取得 * * @param albumName * @return */ private File getSdStorageDir(String albumName) { File dir = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), albumName); if (!dir.exists()) { if (!dir.mkdirs()) { Log.e("Error", "Directory not created"); } } return dir; } /** * ストレージが読み込み可能か * * @return */ public boolean canReadSd() { String status = Environment.getExternalStorageState(); if (!status.equals(Environment.MEDIA_MOUNTED)) { return false; } File file = Environment.getExternalStorageDirectory(); if (file.canRead()) { return true; } return false; } /** * ストレージに書き込み可能か * * @return */ public boolean canWriteSd() { String status = Environment.getExternalStorageState(); if (!status.equals(Environment.MEDIA_MOUNTED)) { return false; } File file = Environment.getExternalStorageDirectory(); if (file.canWrite()) { return true; } return false; } /** * ストレージが使用可能か * * @return */ public boolean canUseSd() { return canReadSd() && canWriteSd(); } } |
使い方は、
1 2 3 4 5 6 7 8 9 10 |
ImageView imageView = (ImageView) findViewById(R.id.photo_image); Bitmap imageForScale = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); ImageManager imageManager = new ImageManager(this); try { String albumName = "Save image sample"; imageManager.save(imageForScale, albumName); } catch (Error e) { Log.e("MainActivity", "onCreate: " + e); } |