2014-05-13 32 views
0

我正在使用这些代码捕获和存储按钮上的图像点击。捕获和存储照片无法正常工作 - Android

takePhoto.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     destination = new File(Environment 
       .getExternalStorageDirectory(), 
       preferences.getSelectedItem().getItemNo() + "_" 
       + preferences.getSelectedItem().getChasisNo() + "_up"); 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, 
       Uri.fromFile(destination)); 
     startActivityForResult(intent, TAKE_PHOTO_CODE); 
    } 
}); 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { 
     FileInputStream in; 
     try { 
      in = new FileInputStream(destination); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inSampleSize = 10; 
      imgPath = destination.getAbsolutePath(); 
      // Bitmap bmp = BitmapFactory.decodeStream(in, null, options); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     Log.d("Image saved", "Image saved at " + imgPath); 
     upItem.setUpPhotoURL(String.valueOf(imgPath)); 
     isPhotoAttached = true; 
    } 
} 

但是当我捕捉照片并尝试确认它时,应用程序什么都不做。取消和重拍选项可以很好地工作,但确认拍摄的图像不会做任何事情。任何人都可以指出问题出在哪里?

+0

看我的答案在这里它会帮助你http://stackoverflow.com/questions/23606330/how-to-distinguish-whether-the-image-is-coming-from-gallery-or-camera-in-android/23606878#23606878 –

+0

问题是,当我按下确认按钮时resultCode不起作用。我试过Log.d(“Test”,String.valueOf(resultCode));但只有当我按下取消按钮(测试:0)时才会创建日志,而当我试图按下确认按钮时没有任何东西。 –

回答

0

,我认为这是行不通的,因为图像的名称是太大了。当我改变了这个

destination = new File(Environment.getExternalStorageDirectory(), 
       preferences.getSelectedItem().getItemNo() + "_" 
       + preferences.getSelectedItem().getChasisNo() + "_up"); 

这个

destination = new File(Environment.getExternalStorageDirectory(), 
       preferences.getSelectedItem().getChasisNo() + ".jpg"); 

我不知道是否有这样的事情作为图像名称是太大,但这种变化是为我工作

0

清单档案中的

<uses-feature android:name="android.hardware.camera" /> 

添加此权限。

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { 
    String[] projection = { MediaStore.Images.Media.DATA}; 
    @SuppressWarnings("deprecation") 
    Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
    int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    String capturedImageFilePath = cursor.getString(column_index_data); 

Toast.makeText(this, capturedImageFilePath, Toast.LENGTH_SHORT).show(); 

} 
+0

我试着编辑Manifest,但根本没有任何效果。然后我试图复制你的onActivityResult,但它说方法managedQuery()已弃用 –

+0

没有问题,你测试它,它工作? – Rohit

+0

不,不工作。它说,在“int col ...”行 –

0

试试这个

String mCurrentPhotoPath = destination.getAbsolutePath(); 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    switch (requestCode) { 
    case TAKE_PHOTO_CODE: { 
     if (resultCode == RESULT_OK) { 

      if (mCurrentPhotoPath != null) { 
       getBitmap(mCurrentPhotoPath); 
      } 
     } 
     break; 
    } 

    default: 

     break; 
    } 

} 

private Bitmap getBitmap(String mCurrentPhotoPath) { 

    /* There isn't enough memory to open up more than a couple camera photos */ 
    /* So pre-scale the target bitmap into which the file is decoded */ 

    /* Get the size of the ImageView */ 
    int targetW = imageView.getMeasuredWidth(); 
    int targetH = imageView.getMeasuredHeight(); 

    /* Get the size of the image */ 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 
    Bitmap oribitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, 
      bmOptions); 
    int photoW = bmOptions.outWidth; 
    int photoH = bmOptions.outHeight; 

    /* Figure out which way needs to be reduced less */ 
    int scaleFactor = 3; 

    /* Set bitmap options to scale the image decode target */ 
    bmOptions.inJustDecodeBounds = false; 
    bmOptions.inSampleSize = scaleFactor; 
    bmOptions.inPurgeable = true; 

    /* Decode the JPEG file into a Bitmap */ 
    return oribitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, 
      bmOptions); 

} 

此权限清单档案中的

<uses-feature android:name="android.hardware.camera" /> 
+0

问题是,resultCode永远不会RESULT_OK。我试着记录resultCode,但是当我按下确认按钮时它从不记录任何东西。当我按下取消时,它记录0 –

+0

改变这一行:case TAKE_PHOTO_CODE:{代替案例CAMERA_REQUEST_CODE:{ – Shini

相关问题