写真を撮ってディレクトリに保存すると思われるAndroidアプリに取り組んでいます。Android 開発者サイト ( http://developer.android.com/training/camera/photobasics.html ) でコードを試してみましたが、完全に機能します。私はそれを少し変更し、アプリでいくつかのコードを使用しましたが、写真ディレクトリの作成に失敗し、Logcat でエラーが発生しました:
09-10 01:32:44.081: D/CameraSample(1477): ディレクトリの作成に失敗しました
ここに私のコードがあります:
public class TakePhoto{
public static final int ACTION_TAKE_PHOTO = 1;
private static AlbumStorageDirFactory mAlbumStorageDirFactory = null;
private static final String JPEG_FILE_SUFFIX = ".jpg";
private static String mCurrentPhotoPath;
private static final String DEBUG_TAG= "PhotoIntent";
private static int counter = 0;
/* private constructor */
private TakePhoto (){ }
public static File StorageDir(String dirName){
File storageDir = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
Log.d(DEBUG_TAG , counter++ + " GetAlbumName: " + dirName);
storageDir = mAlbumStorageDirFactory.getAlbumStorageDir(dirName);
Log.d(DEBUG_TAG , counter++ + " GetAlbumDir: " + storageDir);
if (storageDir != null) {
if (! storageDir.mkdirs()) {
if (! storageDir.exists()){
Log.d("CameraSample", "failed to create directory");
return null;
}
}
}
} else {
Log.v(dirName, "External storage is not mounted READ/WRITE.");
}
return storageDir;
}
public static String saveInDir(String dirName){
File storage = StorageDir(dirName);
if (storage == null){
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (!storage.mkdir())
return null;
}
File imageF;
try {
imageF = File.createTempFile(setUpPicName() , JPEG_FILE_SUFFIX, storage);
Log.d(DEBUG_TAG , counter++ + " CreateImageFile: " + imageF.toString() + " " + imageF.getAbsolutePath());
mCurrentPhotoPath = imageF.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
Log.d(DEBUG_TAG , counter++ + " IOexception: " + mCurrentPhotoPath);
mCurrentPhotoPath = null;
}
return mCurrentPhotoPath;
}
public static void setDirFactory(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
mAlbumStorageDirFactory = new FroyoAlbumDirFactory();
Log.d(DEBUG_TAG , counter++ + " foryoAlbumDirFactory: ");
} else {
mAlbumStorageDirFactory = new BaseAlbumDirFactory();
}
}
行で:
storageDir = mAlbumStorageDirFactory.getAlbumStorageDir(dirName);
getAlbumStorageDir は FroyoAlbumDirFactory.java から呼び出されます。
package beeah.ae.takephoto;
import java.io.File;
import android.os.Environment;
import android.util.Log;
public final class FroyoAlbumDirFactory extends AlbumStorageDirFactory {
@Override
public File getAlbumStorageDir(String albumName) {
Log.d("FroyoFactory" , "Inside froyofactory");
return new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
),
albumName
);
}
}
PS storageDir コンテンツをデバッグすると、次のようになります。
09-10 01:32:43.971: D/PhotoIntent(1477): 2 GetAlbumDir: /storage/sdcard/Pictures/myPictures