メインアクティビティには、カメラにアクセスするインテントを開始するボタン takeApic があり、onActivityResult でビットマップを他のアクティビティ (ShowActivity) に渡しました
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
// 2
Bitmap show_img = (Bitmap) data.getExtras().get("data");
Intent mIntent = new Intent(MainActivity.this,ShowActivity.class);
mIntent.putExtra("BitmapImage", show_img);
startActivity(mIntent);
}
次に、ShowActivity が取得した画像 (インテントを通じて受信したもの) をイメージビューに設定します
Intent mIntent = getIntent();
/** retrieve the string extra passed */
Bitmap our_img = (Bitmap) mIntent.getParcelableExtra("BitmapImage");
mImage.setImageBitmap(our_img);
ShowActivity で [保存] ボタンが押されたときに、画像を保存する必要があります ここに私のコードがあります
FileOutputStream output;
File filepath = Environment.getExternalStorageDirectory();
// Create a new folder AndroidBegin in SD Card
File dir = new File(filepath.getAbsolutePath() + "/Hello/");
dir.mkdirs();
// Create a name for the saved image
File file = new File(dir, pic_name+".png");
// Notify the user on successful save
Toast.makeText(this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
try {
// Image starts saving
output = new FileOutputStream(file);
our_img.compress(Bitmap.CompressFormat.PNG, 100, output);
output.flush();
output.close();
}
// Catch exceptions
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// to see saved images in the gallery view.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
*プログラムは写真を撮ることができます*フォルダーと画像ファイルも作成しますが、作成されたファイルは0KBで、フォルダーはギャラリーに表示されません
psマニフェストで、外部ストレージへの書き込みとカメラへのアクセスの許可を与えました
解決策を教えてください。よろしくお願いします!!!