我有代码来显示来自相机设备的捕捉图像
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String file_name = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
File file = new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + file_name + ".jpg");
mImageCaptureUri = Uri.fromFile(file);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
try {
intent.putExtra("return-data", true);
intent.putExtra("mImageCaptureUri", mImageCaptureUri);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (Exception e) {
e.printStackTrace();
}
并从路径设置图像位图,当我从纵向视图相机设备捕获图像时,它工作,但是当我从横向视图相机捕获图像时出现错误,我认为这是因为我检索图像的活动是肖像。所以你可以给我建议,以便我可以从肖像或风景摄像机捕捉图像?谢谢
答案 0 :(得分:1)
试试这个
首先开始意图
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
然后是结果的活动
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
if(data!=null && resultCode == RESULT_OK)
{
bitMapForProfilePic = (Bitmap) data.getExtras().get("data");
bitMapForProfilePic =Bitmap.createScaledBitmap(bitMapForProfilePic, getWindowManager().getDefaultDisplay().getWidth()/5, getWindowManager().getDefaultDisplay().getHeight()/4, true);
registration_profilePicID.setImageBitmap(bitMapForProfilePic); bitMapForProfilePic=null;
}
}
}
如果你想存储位图
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
OutputStream outStream = null;
File file = new File(extStorageDirectory, "profilepicture.PNG");
try {
outStream = new FileOutputStream(file);
bitMapForProfilePic.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
然后设置图像
view.setImageBitmap(BitmapFactory.decodeFile("/sdcard/profilepicture.PNG"));
如果您有任何疑问,请告诉我......
答案 1 :(得分:0)
我在其中一个应用中遇到过类似的问题,我已按以下方式解决了这个问题: 首先保存您的实例状态:
@Override
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
state.putString(IMAGE_FILE_PATH, imageFilePath); }
然后恢复它:
@Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
ImageFilePath = state.getString(IMAGE_FILE_PATH); }
这适用于我,但变量ImageFilePath应该是您的Activity的全局,所以也许有更优雅的方式来实现这个