Mainのクラス外で複数の画像をCanvasにつなげて描画し、それを端末内に保存するプログラムを書いているのですが、画像を保存するところで ContentResolverを使用しないと端末を再起動しなければギャラリーに表示がされません。
ですが、このプログラムを書いているクラスにはContextを渡していません。その為か、getContentResolver();でエラーが出てしまいます。
なるべくContextを渡さないで解決したいのですがいい方法は無いでしょうか?
Android
1package com.example.admin.saveimage1; 2 3import android.content.ContentResolver; 4import android.content.ContentValues; 5import android.content.Context; 6import android.content.res.Resources; 7import android.graphics.Bitmap; 8import android.graphics.BitmapFactory; 9import android.graphics.Canvas; 10import android.graphics.Paint; 11import android.os.Environment; 12import android.provider.MediaStore; 13 14import java.io.File; 15import java.io.FileInputStream; 16import java.io.FileNotFoundException; 17import java.io.FileOutputStream; 18import java.io.IOException; 19import java.io.InputStream; 20 21/** 22 * Created by admin on 2017/01/23. 23 */ 24public class saveimage { 25 26 MainActivity ma = new MainActivity(); 27 28 29 Resources r; 30 Bitmap pic1; 31 Bitmap pic2; 32 Bitmap pic3; 33 Bitmap pic4; 34 Bitmap pic5; 35 Bitmap pic6; 36 Bitmap pic7; 37 Bitmap pic8; 38 Bitmap pic9; 39// Bitmap bitmap = BitmapFactory.decodeResource(ma.getresource(), R.drawable.source); 40// int width = bitmap.getWidth(); // 元ファイルの幅取得 41// int height = bitmap.getHeight(); // 元ファイルの高さ取得 42// Bitmap bitmap1 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 43// Canvas canvas = new Canvas(bitmap1); 44// 45 46// private Paint paint = new Paint(); 47 Paint paint = new Paint(); 48 String path = Environment.getExternalStorageDirectory().getPath(); 49 File dir = new File(path + "/MyPhoto/"); 50 File file1 = new File(dir.getAbsolutePath() + "/15_29066_12908.jpg"); 51 File file2 = new File(dir.getAbsolutePath() + "/15_29066_12909.jpg"); 52 File file3 = new File(dir.getAbsolutePath() + "/15_29067_12908.jpg"); 53 File file4 = new File(dir.getAbsolutePath() + "/15_29067_12909.jpg"); 54 File file5 = new File(dir.getAbsolutePath() + "/15_29068_12908.jpg"); 55 File file6 = new File(dir.getAbsolutePath() + "/15_29068_12909.jpg"); 56 File file7 = new File(dir.getAbsolutePath() + "/15_29066_12910.jpg"); 57 File file8 = new File(dir.getAbsolutePath() + "/15_29067_12910.jpg"); 58 File file9 = new File(dir.getAbsolutePath() + "/15_29068_12910.jpg"); 59 60 61 62 public void savemap(Context context,Resources resource) { 63 64 Bitmap bitmap = BitmapFactory.decodeResource(resource, R.drawable.source); 65 int width = bitmap.getWidth(); // 元ファイルの幅取得 66 int height = bitmap.getHeight(); // 元ファイルの高さ取得 67 Bitmap bitmap1 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 68 Canvas canvas = new Canvas(bitmap1); 69 70 71 // TODO Auto-generated constructor stub 72 try { 73 InputStream is = new FileInputStream(file1); 74 pic1 = BitmapFactory.decodeStream(is); 75 } catch (FileNotFoundException e) { 76 e.printStackTrace(); 77 } 78 79 80 try { 81 InputStream is = new FileInputStream(file2); 82 pic2 = BitmapFactory.decodeStream(is); 83 } catch (FileNotFoundException e) { 84 e.printStackTrace(); 85 } 86 87 88 try { 89 InputStream is = new FileInputStream(file3); 90 pic3 = BitmapFactory.decodeStream(is); 91 } catch (FileNotFoundException e) { 92 e.printStackTrace(); 93 } 94 95 96 try { 97 InputStream is = new FileInputStream(file4); 98 pic4 = BitmapFactory.decodeStream(is); 99 } catch (FileNotFoundException e) { 100 e.printStackTrace(); 101 } 102 103 104 try { 105 InputStream is = new FileInputStream(file5); 106 pic5 = BitmapFactory.decodeStream(is); 107 } catch (FileNotFoundException e) { 108 e.printStackTrace(); 109 } 110 111 112 try { 113 InputStream is = new FileInputStream(file6); 114 pic6 = BitmapFactory.decodeStream(is); 115 } catch (FileNotFoundException e) { 116 e.printStackTrace(); 117 } 118 119 120 try { 121 InputStream is = new FileInputStream(file7); 122 pic7 = BitmapFactory.decodeStream(is); 123 } catch (FileNotFoundException e) { 124 e.printStackTrace(); 125 } 126 127 128 try { 129 InputStream is = new FileInputStream(file8); 130 pic8 = BitmapFactory.decodeStream(is); 131 } catch (FileNotFoundException e) { 132 e.printStackTrace(); 133 } 134 135 136 try { 137 InputStream is = new FileInputStream(file9); 138 pic9 = BitmapFactory.decodeStream(is); 139 } catch (FileNotFoundException e) { 140 e.printStackTrace(); 141 } 142 143 144 canvas.scale(3, 3); 145 canvas.drawBitmap(pic1, 0, 0, paint); 146 canvas.drawBitmap(pic2, 0, pic1.getWidth(), paint); 147 canvas.drawBitmap(pic3, pic1.getWidth(), 0, paint); 148 canvas.drawBitmap(pic4, pic1.getWidth(), pic1.getWidth(), paint); 149 canvas.drawBitmap(pic5, pic1.getWidth() * 2, 0, paint); 150 canvas.drawBitmap(pic6, pic1.getWidth() * 2, pic1.getWidth(), paint); 151 canvas.drawBitmap(pic7, 0, pic1.getWidth() * 2, paint); 152 canvas.drawBitmap(pic8, pic1.getWidth(), pic1.getWidth() * 2, paint); 153 canvas.drawBitmap(pic9, pic1.getWidth() * 2, pic1.getWidth() * 2, paint); 154 155 156 try { 157 saveBitmap(bitmap1); 158 } catch (Exception e) { 159 e.printStackTrace(); 160 } 161 162 163 } 164 165 166 167 168 public void saveBitmap(Bitmap saveImage) throws IOException { 169 170 final String SAVE_DIR = "/MyPhoto"; 171 File file = new File(Environment.getExternalStorageDirectory().getPath() + SAVE_DIR); 172 173 String fileName = "aaaa" + ".jpg"; 174 String AttachName = file.getAbsolutePath() + "/" + fileName; 175 176 try { 177 FileOutputStream out = new FileOutputStream(AttachName); 178 saveImage.compress(Bitmap.CompressFormat.JPEG, 30, out); 179 out.flush(); 180 out.close(); 181 } catch(IOException e) { 182 e.printStackTrace(); 183 throw e; 184 } 185 186 // save index 187 ContentValues values = new ContentValues(); 188 ContentResolver contentResolver = getContentResolver(); 189 values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 190 values.put(MediaStore.Images.Media.TITLE, fileName); 191 values.put("_data", AttachName); 192 contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 193 194 195 196 } 197 198 199 200} 201 202
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/24 11:05
2017/01/24 11:09
2017/01/24 11:30
2017/01/24 11:41
2017/01/24 11:55
2017/01/30 06:52
2017/01/30 07:09