java 在Android应用程序中保存相机拍摄的照片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15214321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
saving pictures taken by camera in android app
提问by Rav
I have been stuck on this for a while now and have looked at various tutorials for help but have not yet succeeded.
我已经坚持了一段时间,并查看了各种教程寻求帮助,但尚未成功。
I have essentially utilised the camera
function in my appto take picturesand display a preview
of it BUTit can't save taken picture.
我基本上利用了camera
我的应用程序中的功能来拍照并显示其中一张preview
,但它无法保存拍摄的照片。
Here is the java
code containing my attempt to get it functioning according to tutorials:
这是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;
}
}
I have already included all the neccessary permissions
within the Manifest.xml
file.
我已经permissions
在Manifest.xml
文件中包含了所有必要的内容。
回答by Raghunandan
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);
Have a look at this link.How to save images from Camera in Android to specific folder?
看看这个链接。如何将Android相机中的图像保存到特定文件夹?
回答by spartygw
In your onClick
where you call the Intent you should specify the output file by utilizing your existing getOutputMediaFileUri
like this:
在您onClick
调用 Intent 的地方,您应该getOutputMediaFileUri
像这样利用现有文件来指定输出文件:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());
startActivityForResult(i, cameraData);
So now YOU are telling the camera where to save the file rather than asking where it was saved. This is how I got my camera app to work so that I could access the picture after it was snapped.
所以现在您是在告诉相机将文件保存在何处,而不是询问文件保存在何处。这就是我让我的相机应用程序工作的方式,以便我可以在拍摄后访问图片。