0

我正在开发 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

4

2 回答 2

2

请在此处发布您的 logcat 错误,并检查您是否获得了许可

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在您的 manifest.xml 文件中。

于 2013-09-10T06:18:26.440 回答
1

在将图片保存到外部存储之前,您需要通过编写以下代码检查卡是否已安装。所以它会阻止你的应用程序在中间崩溃。

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

if(isSDPresent)
{
  // continue with your picture saving code
}
else
{
 // show message to user
}

希望这会帮助你。:)

于 2013-09-11T10:30:21.310 回答