将照片从相机保存到Android应用程序中的目录失败

时间:2013-09-10 06:00:24

标签: android camera directory

我正在研究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
        );
    }
}

P.S。当我调试storageDir内容时,它给了我:

09-10 01:32:43.971:D / PhotoIntent(1477):2 GetAlbumDir:/ storage / sdcard / Pictures / myPictures

2 个答案:

答案 0 :(得分:2)

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

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在manifest.xml文件中。

答案 1 :(得分: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
}

希望这会对你有所帮助。 :)