Kev*_*vik 56 camera android image
我用按钮创建了一个应用程序,并为该按钮编写了onClickListener.我已经尝试了几个示例代码示例,但没有一个有效.他们都带来了Android相机应用程序,不拍照.我想要一些代码可以放在我的onClickListener中,所以当我按下屏幕上的按钮时,会拍摄一张照片.
当我在Android活动中按下按钮时,如何让相机拍照?
Meh*_*sar 97
请看下面的演示代码.
这是用于UI的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" >
<Button
android:id="@+id/btnCapture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Camera" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是你的Java类文件,
public class CameraDemoActivity extends Activity {
int TAKE_PHOTO_CODE = 0;
public static int count = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Here, we are making a folder named picFolder to store
// pics taken by the camera using this application.
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
Button capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Here, the counter will be incremented each time, and the
// picture taken by camera will be stored as 1.jpg,2.jpg
// and likewise.
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
}
catch (IOException e)
{
}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 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) {
Log.d("CameraDemo", "Pic saved");
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:
在清单文件中指定以下权限,
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用Magical Take Photo库。
1.在gradle中尝试编译
compile 'com.frosquivel:magicaltakephoto:1.0'
Run Code Online (Sandbox Code Playgroud)
2.您需要在manifest.xml中获得此权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
Run Code Online (Sandbox Code Playgroud)
3.实例这样的类
//“ this”是当前的活动参数
MagicalTakePhoto magicalTakePhoto = new MagicalTakePhoto(this,ANY_INTEGER_0_TO_4000_FOR_QUALITY);
Run Code Online (Sandbox Code Playgroud)
4.如果需要拍照使用方法
magicalTakePhoto.takePhoto("my_photo_name");
Run Code Online (Sandbox Code Playgroud)
5.如果需要在设备中选择图片,请尝试以下方法:
magicalTakePhoto.selectedPicture("my_header_name");
Run Code Online (Sandbox Code Playgroud)
6.您需要像这样重写活动或片段的onActivityResult方法:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
magicalTakePhoto.resultPhoto(requestCode, resultCode, data);
// example to get photo
// imageView.setImageBitmap(magicalTakePhoto.getMyPhoto());
}
Run Code Online (Sandbox Code Playgroud)
注意:只有使用此图库,您才能在设备中拍照并选择图片,这使用的是最小API 15。
| 归档时间: |
|
| 查看次数: |
182537 次 |
| 最近记录: |