我已经坚持了一段时间,并且已经查看了各种教程以寻求帮助但尚未成功。
我基本上使用了应用中的camera
功能来拍摄图片并显示preview
但是强>它无法保存拍照。
以下是java
代码,其中包含我根据教程尝试使其正常运行的代码:
public class Activity_Camera extends Activity implements View.OnClickListener {
ImageButton ib;
ImageView iv;
Intent i;
public static final int cameraData = 0;
Bitmap bmp;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Info:
Intent i = new Intent(this, Help.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
initialise();
}
private void initialise() {
iv = (ImageView) findViewById(R.id.ivPicReturn);
ib = (ImageButton) findViewById(R.id.ibTakePic);
ib.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
File mediaFile;
if (type == cameraData){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_" + ".jpg");
} else {
return null;
}
return mediaFile;
}
}
我已在permissions
文件中包含所有必需的Manifest.xml
。
答案 0 :(得分:3)
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
查看此链接。How to save images from Camera in Android to specific folder?
答案 1 :(得分:0)
在您调用Intent的onClick
中,您应该使用现有的getOutputMediaFileUri
来指定输出文件:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());
startActivityForResult(i, cameraData);
所以现在你告诉相机保存文件的位置,而不是询问保存的位置。 这就是我让我的相机应用程序工作的方式,以便我可以在拍摄后访问该图片。