我有一个活动,我希望用户按下视图并拍摄几个地方的照片然后插入每个“按下”的地方。像照片文件夹一样。
所以我创建了一个类来处理拍照和制作位图。
但是我无法弄清楚如何在我需要它的活动中解码位图,我只是得到一个静态无法从非静态错误中引用,因为我没有正确地实例化它我想。
继承我的方法:
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?
答案 0 :(得分:0)
删除sudo service motion restart
。
最有可能的是,您可以替换:
new CameraActivity()
使用:
Bitmap bitmap = BitmapFactory.decodeStream(CameraActivity.openFileInput("myImage"));
因为此Bitmap bitmap = BitmapFactory.decodeStream(openFileInput("myImage"));
似乎是UI代码,因此可能已经在活动中。如果它在片段中,请使用case
。如果它在其他地方使用getActivity().openFileInput()
。