【问题标题】:Capture image from Camera in HTC Desire Android device is not working?从 HTC Desire Android 设备中的相机拍摄图像无法正常工作?
【发布时间】:2012-07-06 13:27:47
【问题描述】:

我正在做一个相机应用程序,它从相机中挑选照片我正在使用以下代码,它在三星 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;

【问题讨论】:

    标签: android


    【解决方案1】:

    如果您在启动相机时传递了 Image 的 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); 
         }    
       } 
    } 
    

    【讨论】:

    • imran 如何在 Android 中处理这些多屏支持应用程序,您有什么帮助吗?
    • @srnu :对不起,亲爱的,我不知道。但如果你把它作为一个问题发布,那么我相信你会很快得到正确的答案。谢谢朋友!!!:)
    • @srnu :如果此答案对您有帮助,您可以将其标记为其他人帮助的答案。谢谢
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      Htc 中没有名为 DCIM/Camera 的文件夹,只是将行更改为正常工作

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

      【讨论】:

        猜你喜欢
        • 2011-07-15
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-28
        • 1970-01-01
        • 2018-06-05
        相关资源
        最近更新 更多