キャンバスに描画して保存すると正しく機能し
ますが、SDカードから画像をロードしたいときに何か問題が発生します(ただし、描画して保存した画像の1つをロードすると、正しく機能します)
ロード画像のコード
case R.id.menu_load:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, REQ_LOAD);*/
return true;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_LOAD:
if (resultCode == RESULT_OK) {
Uri imageUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(imageUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
final File imageFile = new File(cursor.getString(columnIndex));
cursor.close();
String loadMessge = getResources().getString(R.string.dialog_load);
final ProgressDialog load = ProgressDialog.show(TpMainActivity.this, "", loadMessge, true);
Thread thread = new Thread() {
@Override
public void run() {
//File photos= new File(imageFile);
//Bitmap bitmap = decodeFile(imageFile);
Bitmap bitmap = Utils.decodeFile(TpMainActivity.this, imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, null);
paintView.setBitmap(bitmap);
load.dismiss();
}
};
thread.start();
}
break;
}
}
public static Bitmap decodeFile(Context c, File f) {
Bitmap tmpBitmap = null;
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int tmpWidth = o.outWidth, tmpHeight = o.outHeight;
int scale = 1;
while (tmpWidth / 2 > REQUIRED_SIZE || tmpHeight / 2 > REQUIRED_SIZE) {
tmpWidth /= 2;
tmpHeight /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
tmpBitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
// http://sudarnimalan.blogspot.com/2011/09/android-convert-immutable-bitmap-into.html
// this is the file going to use temporally to save the bytes.
File file = new File(c.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "tmp");
file.getParentFile().mkdirs();
// Open an RandomAccessFile
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
// get the width and height of the source bitmap.
int width = tmpBitmap.getWidth();
int height = tmpBitmap.getHeight();
// Copy the byte to the file
// Assume source bitmap loaded using options.inPreferredConfig = Config.ARGB_8888;
FileChannel channel = randomAccessFile.getChannel();
MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, width * height * 4);
tmpBitmap.copyPixelsToBuffer(map);
// recycle the source bitmap, this will be no longer used.
tmpBitmap.recycle();
// Create a new bitmap to load the bitmap again.
tmpBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
map.position(0);
// load it back from temporary
tmpBitmap.copyPixelsFromBuffer(map);
// close the temporary file and channel , then delete that also
channel.close();
randomAccessFile.close();
file.delete();
} catch (FileNotFoundException e) {
Log.e(TpApplication.TAG, "ERROR ", e);
} catch (IOException e) {
Log.e(TpApplication.TAG, "ERROR ", e);
}
return tmpBitmap;
}