如何从相机拍摄图像中的Android?
我做了一个应用程序从相机拍摄的照片。
I have made an application to capture photos from the camera.
我已经创建了两个活动:活动1有一个按钮,启动相机wehen被点击。当图像被捕获,它被传递给活性2。
I have created two Activities: In Activity1 there is one Button which starts the camera wehen it is clicked. When the image is captured, it is passed to Activity2.
然而,当我运行应用程序并启动活动1(与一个Button),我按一下按钮来启动它显示出消息不幸的是,相机已停止弹出窗口的摄像头。有在日志猫或在控制台上没有任何错误。
However, when I run the application and start the Activity1 (with the one Button) and I click on the button to start the camera it displays a pop up window showing the message "Unfortunately, camera has stopped". There are no errors in the log-cat or on the console.
谁能帮助我。请。非常感谢。
Can anyone help me. Please. Thanks a lot.
请尝试以下code:
package com.example.sample1;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class CapturePhotoSample1 extends Activity implements OnClickListener
{
public static final int TAKE_PHOTO=1;
ImageView imageView=null;
private File folder;
String imageFileName=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_capture_photo_sample1);
Button button=(Button)this.findViewById(R.id.capture_button);
button.setOnClickListener(this);
button=null;
imageView=(ImageView)this.findViewById(R.id.image_view1);
folder = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES)+ "/sample1/");
if (!folder.exists()) {
folder.mkdir();
}
}
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent,actionCode);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id=v.getId();
if(id==R.id.capture_button)
this.dispatchTakePictureIntent(TAKE_PHOTO);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu1, menu);
return true;
}
public void handleCameraPhoto(Intent intent)
{
Bundle extras=intent.getExtras();
Bitmap bitmap=(Bitmap)extras.get("data");
this.imageView.setImageBitmap(bitmap);
if(this.isExternalStorageAvailable())
{
this.imageFileName="img_"+SDUtil.now(-1)+".png";
/*SDUtil.now() is our own library.It is for creating file name with respect to data and time.IF u copy the hole program means sdutil shows error.For that you write a logic for creating a file name. */
String path=folder+"/"+this.imageFileName;
FileOutputStream fos=null;
BufferedOutputStream bos=null;
try
{
fos=new FileOutputStream(path);
bos=new BufferedOutputStream(fos);
bitmap.compress(Bitmap.CompressFormat.PNG, 40, bos);
}
catch(Exception ex)
{
ex.printStackTrace();
}
if(bos!=null)
{
try
{
bos.flush();
//bos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
if(bos!=null)
{
try
{
bos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
if(fos!=null)
{
try
{
fos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
bos=null;
fos=null;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==Activity.RESULT_OK)
{
if(requestCode==TAKE_PHOTO)
{
handleCameraPhoto(data);
}
}
}
private boolean isExternalStorageAvailable() {
StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
.getPath());
double sdAvailSize = (double) stat.getAvailableBlocks()
* (double) stat.getBlockSize();
// One binary gigabyte equals 1,073,741,824 bytes.
double mbAvailable = sdAvailSize / 1048576;
String state = Environment.getExternalStorageState();
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but
// all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
if (mExternalStorageAvailable == true
&& mExternalStorageWriteable == true && mbAvailable > 10) {
return true;
} else {
return false;
}
}
}