我从我的片段开始标准的Android相机应用程序。一切正常,图像按照我的意愿保存。用户拍完照片后(仅此时)我想启动一个DialogFragment。
我尝试使用意图并从同一个片段调用onActivityResult()
。但是 MyCamera camera = new MyCamera(getActivity());
camera.start();
永远不会被调用。
我如何在片段中启动相机:
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
activity.startActivityForResult(takePictureIntent, Constants.TAKE_PHOTO);
我的MyCamera课我是这样开始的意图:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
// do something
}
}
回到我调用onActivityResult()的片段中:
public class MyCamera {
private Activity activity;
private static final String TAG = "MyCamera";
private String currentPhotoPath;
private String currentFileName;
private File currentFile;
private Uri contentUri;
public MyCamera(Activity activity) {
this.activity = activity;
}
public void start() {
dispatchPictureIntent();
addPictureToGallery();
currentPhotoPath = null;
}
private void dispatchPictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// check if device has camera
if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
currentPhotoPath = photoFile.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(activity, R.string.msg_picture_not_saved, Toast.LENGTH_LONG).show();
}
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
takePictureIntent.putExtra(Constants.IMAGE_FILE_NAME, currentFileName);
activity.startActivityForResult(takePictureIntent, Constants.TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// set up storage dir
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (!storageDir.exists()) {
if (!storageDir.mkdirs()) {
Log.e(TAG, "failed to create directory");
return null;
}
}
// set up file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fileName = "img_" + timeStamp + "_";
String suffix = ".jpg";
File image = File.createTempFile(fileName, suffix, storageDir);
currentFileName = image.getName();
currentFile = image;
return image;
}
private void addPictureToGallery() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
contentUri = getUri(currentPhotoPath);
mediaScanIntent.setData(contentUri);
activity.sendBroadcast(mediaScanIntent);
}
private Uri getUri(String currentPhotoPath) {
File f = new File(currentPhotoPath);
return Uri.fromFile(f);
}
}
我怎样才能完成这项任务?在此先感谢您的帮助!
这里是整个MyCamera课程:
sudo iptables -t nat -A POSTROUTING -o bond0 -j MASQUERADE
sudo iptables -A FORWARD -i bond0 -o docker0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i docker0 -o bond0 -j ACCEPT
答案 0 :(得分:2)
根据这篇文章:onActivityResult is not being called in Fragment
尝试添加您的托管活动
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
它允许将未处理的活动结果传递给您的片段。
另外,请务必致电fragment.startActivityForResult()
而不是activity.startActivityForResult()
。
答案 1 :(得分:1)
我尝试使用Intent并从同一个片段调用onActivetyResult()。但onActivityResult()永远不会被调用。
这是因为您在startActivityForResult()
上呼叫Activity
,而不是在片段上。您的onActivityResult()
方法会转到您调用startActivityForResult()
的对象上。因此,如果您希望片段具有onActivityResult()
,请在片段上调用startActivityForResult()
。