这是我用来创建意图的代码:
try {
file = createImageFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(file!= null)
{
Log.d(Tag,"absolute path: "+imageAbsolutePath);
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(file));
startActivityForResult(i, TAKE_PICTURE);
}
这是返回具有自定义名称和位置的文件的方法:
private File createImageFile() throws IOException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String currentDate = sdf.format(new Date());
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
String currentTime = sdf2.format(new Date());
String timeArray[] = currentTime.split(":");
String imageName = currentDate+" "+timeArray[0]+"-"+timeArray[1]+"-"+timeArray[2];
// create a File object for the parent directory
File Pictures = new File(Environment.getExternalStorageDirectory()+File.separator+"Pictures");
// have the object build the directory structure, if needed.
if (!Pictures.exists()) {
Pictures.mkdirs();
Log.d(Tag,"Pictures folder created: "+Pictures);
}else
{
Log.d(Tag,"Pictures folder Already Exists: "+Pictures);
}
// File image = File.createTempFile(imageName,".jpg",Pictures);
File image = new File (Pictures,imageName+".jpg" );
Log.d(Tag,"This is Image name: "+ imageName);
imageAbsolutePath = image.getAbsolutePath();
return image;
}
这是我使用的 inActivityResult 方法:
case TAKE_PICTURE:
if(resultCode == RESULT_OK)
{
galleryAddPic();
Bitmap imgBitmap=null;
try
{
imgBitmap = lessResolution(imageAbsolutePath);
}catch (Exception e)
{
e.printStackTrace();
}
if (imgBitmap!=null)
{
images.add(imgBitmap);
GridView grid = (GridView) findViewById(R.id.grid_view);
grid.setAdapter(new ImageAdapter(this));
}
所有这些方法都适用于以下设备:摩托罗拉 Xoom、摩托罗拉 Droid、Razor、一些三星设备,但在三星 Galaxy Ace s5830m (Android 2.3.6) 中不起作用。它正确拍摄图像,但是当它返回 onActivityResult 时没有任何反应。经过一些测试,我发现它没有显示任何内容,因为图像保存在 DCIM/Camera 文件夹而不是图片文件夹中,所以我做错了什么?