我正在开发 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);
从 FroyoAlbumDirFactory.java 调用 getAlbumStorageDir:
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