私は、ユーザーがビューを押して、いくつかの場所を撮影し、それらを "押す"それぞれの場所に挿入するという活動をしています。写真フォルダのように。BitmapFactoryのデコードストリームで使用するアクティビティをインスタンス化する適切な方法はありますか?
私は写真を撮影してビットマップを作成するためのクラスを作っています。
しかし、私はそれが私が推測することはできませんので、私は静的なエラーから参照することはできません、私はそれが必要な活動のビットマップをデコードする方法を把握することはできません。相続人
私のアプローチ:このクラスを呼び出す
case R.id.pButton2:
new CameraActivity();
Bitmap bitmap = BitmapFactory.decodeStream(CameraActivity.openFileInput("myImage"));
mImageView = (ImageView)findViewById(R.id.imageView2);
mImageView.setImageBitmap(bitmap);
mImageView.setRotation(180);
mImageView.setVisibility(View.VISIBLE);
break;
:
public class CameraActivity extends Activity{
private String mCurrentPhotoPath;
private ImageView mImageView;
private Bitmap mImageBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dispatchTakePictureIntent();
}
public void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
System.out.println("ERR CANNOT CREATE FILE");
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
createImageFromBitmap(mImageBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String createImageFromBitmap(Bitmap bitmap) {
String fileName = "myImage";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
return fileName;
}
}
イムこれからのサンプルを使用しました。 how do you pass images (bitmaps) between android activities using bundles?
'new CameraActivity()' - これはまったく機能しません。あなたは 'Intent'を介して' Activity'を起動し、決してそのコンストラクタからは起動しません。 – PPartisan
私も意図を作ってみましたが、どちらもうまくいきません。 – NicklasN