我有一个活动 A,其中有一个按钮和一个图像视图。当我单击按钮时,它应该加载相机并将图像保存在图像视图中。任何人都可以为此提供代码。
提前致谢 !!!
我有一个活动 A,其中有一个按钮和一个图像视图。当我单击按钮时,它应该加载相机并将图像保存在图像视图中。任何人都可以为此提供代码。
提前致谢 !!!
请参阅下面的代码..它可能会对您有所帮助。
把这两行放在你的按钮点击..
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
在您的代码中创建此方法..
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
if (data != null) {
photo = (Bitmap) data.getExtras().get("data");
add_image.setImageBitmap(photo); /* this is image view where you want to set image*/
Log.d("camera ---- > ", "" + data.getExtras().get("data"));
}
}
}
..................................................... .........................................................
下面的代码为您提供相机拍摄的 LastImageID。这只是您想要孔图像的额外代码,然后它对您很有用。
..................................................... ...................................................
private String getLastImageId() {
final String[] imageColumns = { MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA };
final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
Cursor imageCursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns,
null, null, imageOrderBy);
if (imageCursor.moveToFirst()) {
int id = imageCursor.getInt(imageCursor
.getColumnIndex(MediaStore.Images.Media._ID));
String fullPath = imageCursor.getString(imageCursor
.getColumnIndex(MediaStore.Images.Media.DATA));
imageCursor.close();
return fullPath;
} else {
return "no path";
}
}
您可以创建一个方法来捕获图像并在按钮的 onClickListener 中调用它,如下所示:
public void takePicture(View view){
try{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
} catch(Exception e){
e.printStackTrace();
}
}
然后像这样覆盖 onActivityResult 方法:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
case CAMERA_PIC_REQUEST:
String path=null;
try{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
thumbnail = Bitmap.createScaledBitmap(thumbnail,yourDesiredImageWidth, yourDesiredImageHeight, true);
ImageView imageView = (ImageView) findViewById(R.id.yourImageView);
image.setImageBitmap(thumbnail);
break;
}catch(Exception ee){
ee.printStackTrace();
}
}
}
您的活动代码
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button btnTakePhoto;
ImageView imgTakenPhoto;
private static final int CAM_REQUREST = 1313;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnTakePhoto = (Button) findViewById(R.id.button1);
imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);
btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
if (data != null) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imgTakenPhoto.setImageBitmap(thumbnail);
}
}
}
class btnTakePhotoClicker implements Button.OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
}
}
你的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Press the button, capture photo and enjoy!!"
android:id="@+id/textview1"/>
<Button
android:text="Take Photo!"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview1">
</Button>
<ImageView
android:id="@+id/imageView1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:src="@drawable/icon"
android:scaleType="centerCrop"
android:layout_below="@+id/button1">
</ImageView>
</RelativeLayout>
CemraImageActivity.java
public class CemraImageActivity extends Activity implements OnClickListener{
private int CAMERA_REQUEST;
private ImageView imageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.image_capture);
imageView.setOnClickListener(this);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode != 0)
{
if (requestCode == CAMERA_REQUEST)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="334dp"
android:layout_height="wrap_content"
android:text="Click this image for take a photo"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/image_capture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.06"
android:src="@drawable/ic_launcher" />
</LinearLayout>
获取单击按钮并在 imageview 中设置商店图像的非常简单的方法。
`公共类 MainActivity 扩展 Activity {
private static final int CAMERA_PIC_REQUEST = 22;
Uri cameraUri;
Button BtnSelectImage;
private ImageView ImgPhoto;
private String Camerapath ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImgPhoto = (ImageView) findViewById(R.id.imageView1);
BtnSelectImage = (Button) findViewById(R.id.button1);
BtnSelectImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
try {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Couldn't load photo", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public void onActivityResult(final int requestCode, int resultCode, Intent data) {
try {
switch (requestCode) {
case CAMERA_PIC_REQUEST:
if (resultCode == RESULT_OK) {
try {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImgPhoto.setImageBitmap(photo);
} catch (Exception e) {
Toast.makeText(this, "Couldn't load photo", Toast.LENGTH_LONG).show();
}
}
break;
default:
break;
}
} catch (Exception e) {
}
}
}`
设置此清单。
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>
尝试如下:
STEP 1:启动相机拍照:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 2);
第 2 步:从onActivityResult
数据中获取图像
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView=(ImageView) findViewbyId(R.id.imgv);
imageView.setImageBitmap(photo);
}
编辑:第二个解决方案:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")));
startActivityForResult(intent, 2);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 2) {
File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
ImageView imgView=(ImageView)findViewById(R.id.imgView);
Uri imgUri=Uri.fromFile(picture);
imageView.setImageURI(imgUri);
}
}
在 mainactivity.java 中执行此操作后,您需要在清单中执行此操作
public class MainActivity extends Activity {
Button button;
ImageView photo;
static final int Cam_Request = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btnPhoto);
photo = (ImageView)findViewById(R.id.ivPhoto);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_intent, Cam_Request);
}
});
}
private File getFile ()
{
File folder = new File("sdcard/camera_app");
if(!folder.exists())
{
folder.mkdir();
}
File image = new File(folder,"image.jpg");
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "sdcard/camera_app/image.jpg";
photo.setImageDrawable(Drawable.createFromPath(path));
}
}