2012-08-29 23 views
5

我从我的应用程序使用手机的相机捕获图像,并使用android.provider.MediaStore.ACTION_IMAGE_CAPTURE。使用手机摄像头,捕获图像活动后不返回到特定活动

点击捕捉按钮后,显示保存或放弃。点击保存按钮,它会返回到相机而不是应用程序。并且图像保存在gallary而不是我已经声明的输出文件中。

下面是我的代码

如果任何解决方案.. 在此先感谢...

编辑1

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    Intent in=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(in, 0); 

} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
// TODO Auto-generated method stub 
super.onActivityResult(requestCode, resultCode, data); 
if (resultCode==RESULT_OK) { 
    Bitmap bm=(Bitmap) data.getExtras().get("data"); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    bm.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 

    //you can create a new file name "test.jpg" in sdcard folder. 
    File f = new File(Environment.getExternalStorageDirectory() 
          + File.separator + "player1.jpg"); 
    try { 
     f.createNewFile(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    //write the bytes in file 
    FileOutputStream fo = null; 
    try { 
     fo = new FileOutputStream(f); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     fo.write(bytes.toByteArray()); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }finally{ 
     Intent pic=new Intent(pic1.this, GameMenu.class); 
     startActivity(pic); 
     finish(); 
    } 
} 
else { 
    Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_LONG).show(); 
    Intent pic=new Intent(pic1.this, Menu.class); 
    startActivity(pic); 
    finish(); 
} 

请提示:我已经检查了三星GalaxyŸ二重奏代码(Android 2.3的。 6)它不能正常工作。并检查相同的星系流行(2.2.1)和HTC野火(2.3.5)其工作完美。

+0

您是否尝试检查onActivity结果中的响应代码? –

回答

2

使用下面意图捕捉的图像和G etTempFile()就是你提到的存储路径

Intent photoPickerIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
         photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile()); 

         photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString()); 
         photoPickerIntent.putExtra("return-data", true); 
         startActivityForResult(photoPickerIntent,TAKE_PICTURE); 

getTempFile:

private Uri getTempFile() { 


     File root = new File(Environment.getExternalStorageDirectory(), "Directory"); 
     if (!root.exists()) { 
      root.mkdirs(); 
     } 
      File file; 

      file = new File(root,filename+".jpeg"); 

      muri = Uri.fromFile(file); 
      photopath=muri.getPath(); 

       Log.e("getpath",muri.getPath()); 
       return muri; 

      } 

Read here为多个线程

+0

thnx哥们,但结果是相同的星系二重奏否则工作正常。 –

+0

星系二重奏中发生了什么? – Abhi

+0

不返回到应用程序。它仅停留在相机上。 –

0

如果这是你省略了整个代码setContentView();可能是它没有返回的原因。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    // setContentView() here 
    Intent in=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(in, 0); 

} 

是否调用了onActivityResult()方法?

+1

不工作!是的,调用onActivityResult()方法。 –

0

我得到了同样的问题,我已重新启动我的设备,并测试我的应用程序现在工作正常。不同的代码实现应用程序可能没有响应。尝试重新启动您的设备并测试并让我知道它的工作与否

0

您应该创建文件以打开相机。你给的文件URI作为额外的相机意图:

/** 
* Capturing Camera Image will lunch camera app request image capture 
*/ 
private void captureImage() { 

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    fileUri = getOutputMediaFileUri(); 

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

    // start the image capture Intent 
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 
} 

这是我创造的文件:

/** 
* Creating file uri to store image 
* 
* 
* @return Uri 
*/ 
public Uri getOutputMediaFileUri() { 
    return Uri.fromFile(getTempOutputMediaFile()); 
} 

/** 
* returning File 
* 
* 
* @return File 
*/ 
private File getTempOutputMediaFile() { 

    File appDirectory = this.getFilesDir(); 

    File mediaStorageDir = new File(APP_NAME, 
       YOUR_IMAGE_DIRECTORY_NAME); 

    Log.i("mediaStorageDir", mediaStorageDir.getAbsolutePath()); 

    // Create the storage directory if it does not exist 
    if (!mediaStorageDir.exists()) { 
     if (!mediaStorageDir.mkdirs()) { 
      Log.i(YOUR_IMAGE_DIRECTORY_NAME, 
        "Oops! Failed create " 
          + YOUR_IMAGE_DIRECTORY_NAME 
          + " directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    UUID temporaryImageUuid = UUID.randomUUID(); 
    File mediaFile; 

    mediaFile = new File(mediaStorageDir.getPath() + File.separator 
      + temporaryImageUuid + "." + YOUR_EXTENSION); 

    return mediaFile; 
} 
+0

我需要将我的图像文件直接放在内部存储路径 - getFilesDir/Appname中,而不是在外部存储器中。 – joao2fast4u

+0

我更新了答案,图片保存在本地存储中。 –

+0

主要问题是并不总是在onActivityResult中给出三星设备的结果...为什么?我只想找出原因。但是,谢谢你的努力。 – joao2fast4u

0

喜检查code.after拍摄图像的使用startPreview()来释放以下。它适用于我

 captureButton.setOnClickListener(new View.OnClickListener() { 
         @Override 
      public void onClick(View v) { 
          mCamera.autoFocus(null); 
       mCamera.takePicture(null, null, mPicture); 
       Log.e("in capture button","onclick of this button"+ mCamera); 
       mCamera.startPreview(); 
           } 
     }); 
+0

什么时候使用这段代码?哪里是captureButton? – joao2fast4u

+0

意味着在这里我不直接调用相机使用intent.first创建一个按钮,然后实现这个code.it是类似于自定义相机。我们可以添加我们所需的功能。 – image