これは、インテントを作成するために使用するコードです:
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));
}
この方法はすべて、Motorola Xoom、Motorola Droid、Razor、一部の Samsung デバイスなどのデバイスで機能しますが、たとえば、Samsung Galaxy Ace s5830m (Android 2.3.6) では機能しません。画像は正しく取得されますが、onActivityResult に戻っても何も起こりません。いくつかのテストの後、画像は DCIM/Camera フォルダーに保存され、Pictures フォルダーには保存されないため、何も表示されないことがわかりました。