Java 如何在Android中从片段中获取摄像机图像
在我的代码中,位图为null,但图像创建成功,uri正常,但BitmapFactory.decodeFile()返回null 我使用了下面的代码片段:Java 如何在Android中从片段中获取摄像机图像,java,android,android-fragments,android-camera,Java,Android,Android Fragments,Android Camera,在我的代码中,位图为null,但图像创建成功,uri正常,但BitmapFactory.decodeFile()返回null 我使用了下面的代码片段: public class CameraImage extends Fragment { private static final int TAKE_PHOTO_CODE = 1888; Button button; private Uri fileUri1,fileUri; ImageView imageView; @Override pub
public class CameraImage extends Fragment {
private static final int TAKE_PHOTO_CODE = 1888;
Button button;
private Uri fileUri1,fileUri;
ImageView imageView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.camera_image,
container, false);
button = (Button) rootView.findViewById(R.id.button);
imageView = (ImageView) rootView.findViewById(R.id.imageview);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
fileUri1 = getOutputMediaFileUri();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PHOTO_CODE) {
if (resultCode == Activity.RESULT_OK) {
BitmapFactory.Options options;
try {
Bitmap bitmap = BitmapFactory.decodeFile(pathname);
showDialog(bitmap, 1);
} catch (OutOfMemoryError e) {
try {
options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(pathname, options);
imageView.setImageBitmap(bitmap);
} catch (Exception e2) {
Log.e("Camera", "" + e2 + " " + e);
}
}
}
}
} }
public Uri getOutputMediaFileUri() {
return Uri.fromFile(getOutputMediaFile());
}
private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(MyApplication.getAppContext().getFilesDir().getAbsolutePath()),
IMAGE_DIRECTORY_NAME);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
try {
mediaFile = File.createTempFile(
"IMG_" + timeStamp,
".jpg",
mediaStorageDir
);
} catch (Exception e) {
Log.e("Camera", "" + e);
mediaFile = new File(mediaStorageDir.getPath() + File.separator
, "IMG_" + timeStamp + ".jpg");
}
return mediaFile;
}
在上面的代码中,我在onActivityResult中的位图上得到null,但我从该图像中得到的uri位于该位置。我解决了我的问题:
通过更改cameraIntent.putExtra(MediaStore.EXTRA\u输出,fileUri)代码>
到
我们可以通过相机获得两种类型的图像: 1) 在onActivityResult中使用以下代码来缩略图像:
Bitmap photo = (Bitmap) data.getExtras().get("data");
Log.i(TAG, "" + photo + " / " + data.getExtras().get("data") + " / " + data.getData());
showDialog(photo, 1);
BitmapFactory.Options options;
options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(pathname, options);
showDialog(bitmap, 1);
2) 通过在onActivityResult中使用以下代码实现全质量的iMage:
Bitmap photo = (Bitmap) data.getExtras().get("data");
Log.i(TAG, "" + photo + " / " + data.getExtras().get("data") + " / " + data.getData());
showDialog(photo, 1);
BitmapFactory.Options options;
options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(pathname, options);
showDialog(bitmap, 1);
3) 轻松舒适地使用第三方:
编译'com.mindWorks:paracamera:0.2.2'
只是我弄错了您是否有另一个名为
fileUri
的变量?这不是你在这里发布的代码。当你在变量名的末尾有数字时,你可能做错了什么。在这种情况下,您需要创建更多描述性名称。您尤其应该注意,每个变量都是不同的,并且易于识别。你不应该让变量名如此相似以至于你很容易把它们混在一起。此外,你应该尽可能喜欢局部变量而不是成员字段。因为fileUri1
只在onCreateViewI()中使用
像最初一样在那里声明它。最有可能的是fileUri
也在单个方法中使用,而不是在方法之间共享,因此它也应该是本地的。此更改将帮助您避免出现问题发布前出现的错误。