gpt4 book ai didi

android - 相机在 Android 应用程序上卡住

转载 作者:行者123 更新时间:2023-11-30 03:38:52 33 4
gpt4 key购买 nike

我目前正在尝试将相机集成到我的 Android 应用程序中并将图片保存在我的设备上;然而,当我拍照时,我无法继续(该应用程序可以运行,但相机镜头卡住意味着我可以拍摄另一张照片并返回,但无法确认)。我想知道你们是否可以提供帮助。请尽可能简单地解释这一点,因为我是 android 编程的新手。谢谢大家!

public class PhotoCapture extends Activity {
int TAKE_PHOTO_CODE = 0;
public static int count=0;
public static File newfile;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.photocapture);



Button capture = (Button) findViewById(R.id.takepicture);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

ContextWrapper cw = new ContextWrapper(getBaseContext());
newfile = cw.getDir("test.jpg", Context.MODE_PRIVATE);

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) {


uri = data.getData().toString();
Log.d("CameraDemo", "Pic saved");

Intent myview = new Intent(this, Finalpiece.class);
startActivity(myview);


}
}
}

最佳答案

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.CAMERA"/>

之前使用此权限...

private static final int REQ_CAPTURE_CAPTURE__IMAGE = 0;
private String imagePath;

/**
* Start camera activity.
*/
protected void startCameraActivity() {
String imageDirectory = Environment.getExternalStorageDirectory()
+ File.separator + "surbeyImg";
File imageDirectory = new File(imageDirectory);

if (!(imageDirectory.exists())) {
imageDirectory.mkdir();
}

String imagePath = imageDirectory + File.separator + imgName+".jpg");

File file = new File(imagePath);
Uri outputFileUri = Uri.fromFile(file);

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
startActivityForResult(intent, REQ_CAPTURE_CAPTURE__IMAGE);
}

protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {

switch (requestCode) {

case REQ_CAPTURE_CAPTURE__IMAGE:

if (resultCode == RESULT_OK) {
Intent intentImg = new Intent(CameraDemo.this, ShowImg.class);
intentImg.putExtra("imagePath",imagePath);
startActivity(intentImg);

}

break;

default:
break;

}
}

在此之后使用此代码...谢谢

关于android - 相机在 Android 应用程序上卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16160582/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com