0

我正在做一个相机应用程序,它从相机中挑选照片我正在使用以下代码,它在三星 Galaxy ace 中工作,但在带有 Android OS 2.2 的 Htc Desire 中它不起作用,请帮助我解决它,提前致谢。

**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;
4

3 回答 3

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); }

于 2012-07-06T05:13:22.233 回答
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); 
     }    
   } 
} 
于 2012-07-06T05:03:23.393 回答
0

感谢所有。我解决了这个问题,我对我的代码做了一个小改动,我正在使用 File myDirectory = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/") 在 SD 卡中创建文件;

Htc 中没有名为 DCIM/Camera 的文件夹只是将行更改为如下所示

File myDirectory = new File(Environment.getExternalStorageDirectory() + "/DCIM/");

于 2012-07-06T05:27:47.407 回答