我正在做一个相机应用程序从相机中选择照片我正在使用以下代码在三星Galaxy ace中工作但在Htc Desire中使用Android OS 2.2它无法正常工作,请帮助我解决它,提前致谢
**capture button Onclick:**
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
String fileName = "IMG_" + simpleDateFormat.format(new Date()) + ".jpg";
File myDirectory = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/");
cameraImageFile = new File(myDirectory, fileName);
Uri imageUri = Uri.fromFile(cameraImageFile);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
**OnActivityResult:**
switch (requestCode) {
case CAMERA_PIC_REQUEST:
ImageView.setImageBitmap(decodeFile(cameraImageFile.getAbsolutePath()));
}
break;
答案 0 :(得分:0)
如果您在启动相机时传递图像的Uri,则获取onActivityResult
中的图像:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (CAMERA_PIC_REQUEST == resultCode) {
ImageView iv = (ImageView) findViewById(R.id.ReturnedImageView);
// Decode it for real
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = false;
//imageFilePath image path which you pass with intent
Bitmap bmp = BitmapFactory.decodeFile(cameraImageFile, bmpFactoryOptions);
// Display it
iv.setImageBitmap(bmp);
}
}
}
答案 1 :(得分:0)
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String dateNow = formatter.format(currentDate.getTime());
imageName = dateNow + ".jpg";
//Create path to store image in SDCard
path = Environment.getExternalStorageDirectory() + File.separator
+ imageName;
startCameraActivity();
protected void startCameraActivity() {
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent,
Globals.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
switch (resultCode) {
case RESULT_CANCELED:
Log.i("MakeMachine", "User cancelled");
Toast.makeText(getBaseContext(), "User cancelled",
Toast.LENGTH_LONG).show();
break;
case RESULT_OK:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
image.setImageBitmap(bitmap);
}
答案 2 :(得分:0)
感谢all.i解决了这个问题,我对我的代码进行了一些小改动,我正在使用sd卡创建文件 文件myDirectory =新文件(Environment.getExternalStorageDirectory()+“/ DCIM / Camera /”);
Htc中没有名为DCIM / Camera的文件夹,只是将其更改为工作正常以下
文件myDirectory =新文件(Environment.getExternalStorageDirectory()+“/ DCIM /”);