对不起任何语法错误。
我想在与mainActivity不同的活动中使用相机。 我在google Taking Photos Simply上找到了这个简单的教程,说我必须使用这个函数:
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
但是,此函数仅适用于MainActivity,事实上,如果我将此代码放在不同的活动中,我会收到此错误: 无法解决方法' getPackageManager()'。
一些想法?
谢谢!
答案 0 :(得分:0)
尝试以下代码:
static final int REQUEST_IMAGE_CAPTURE = 1;
Context c;
private void dispatchTakePictureIntent() {
Fragment yourFragment = this;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
yourFragment.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
覆盖您的父Activity的onActivityResult():
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
然后将其添加到您的片段中:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == Activity.RESULT_OK) {
//Do something with your captured image. EX:-
try {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath))
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
答案 1 :(得分:0)
对不起,我不知道为什么当我在Nexus 5X上试用这个应用程序时,照片在ImageView中出现时会失去质量。应用图片(图片浏览):i.imgur.com/VunFaHF.png?1相机图片:i.imgur.com/wjJ2a4w.jpg