我正在使用手机的相机并使用android.provider.MediaStore.ACTION_IMAGE_CAPTURE从我的应用程序中捕获图像.
单击捕获按钮后,它会显示保存或丢弃.单击保存按钮,它将返回相机而不是应用程序.并且图像保存在gallary中而不是我声明的输出文件中.
以下是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent in=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(in, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK) {
Bitmap bm=(Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "player1.jpg");
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//write the bytes in file
FileOutputStream fo = null;
try {
fo = new FileOutputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
Intent pic=new Intent(pic1.this, GameMenu.class);
startActivity(pic);
finish();
}
}
else {
Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_LONG).show();
Intent pic=new Intent(pic1.this, Menu.class);
startActivity(pic);
finish();
}
Run Code Online (Sandbox Code Playgroud)
请提出是否有任何解决方案..提前致谢...
编辑1:我已经在三星galaxy y duos(android 2.3.6)上检查了这段代码,但它无法正常工作.并且在galaxy pop(2.2.1)和HTC野火(2.3.5)上检查相同,它的工作完美.
使用下面的意图来捕获图像,getTempFile()是您提到的存储路径
Intent photoPickerIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());
photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());
photoPickerIntent.putExtra("return-data", true);
startActivityForResult(photoPickerIntent,TAKE_PICTURE);
Run Code Online (Sandbox Code Playgroud)
获取临时文件:
private Uri getTempFile() {
File root = new File(Environment.getExternalStorageDirectory(), "Directory");
if (!root.exists()) {
root.mkdirs();
}
File file;
file = new File(root,filename+".jpeg" );
muri = Uri.fromFile(file);
photopath=muri.getPath();
Log.e("getpath",muri.getPath());
return muri;
}
Run Code Online (Sandbox Code Playgroud)
阅读此处了解更多主题
| 归档时间: |
|
| 查看次数: |
2038 次 |
| 最近记录: |