自定义相机应用程序,但无法创建存储图像的文件夹

时间:2013-12-21 06:40:11

标签: android

我想创建一个文件夹来存储捕获的图像。然而,从中获取图像,不创建文件目录,并且一旦我倾斜设备,图像将永久消失。我该怎么办?我在网上寻找了很多例子,因而得到了这样的解决方案。我是Android平台的新手,但我正在学习。任何帮助表示赞赏!谢谢=)

public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);             
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });
}


protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == CAMERA_REQUEST && resultCode == RESULT_OK){
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);    
        File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"imageView");
        if (!directory.exists()) {
            directory.mkdirs();
        }
    }

}

static public boolean hasStorage(boolean requireWriteAccess) {
    //TODO: After fix the bug,  add "if (VERBOSE)" before logging errors.
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        if (requireWriteAccess) {
            boolean writable = checkFsWritable();
            return writable;
        } else {
            return true;
        }
    } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;

}

private static boolean checkFsWritable() {
    // TODO Auto-generated method stub
    return false;
}

}

2 个答案:

答案 0 :(得分:3)

如果您仍然需要访问SD卡,则检查SD卡是否已安装的相机应用程序如下:

    static public boolean hasStorage(boolean requireWriteAccess) {
        //TODO: After fix the bug,  add "if (VERBOSE)" before logging errors.
        String state = Environment.getExternalStorageState();
        Log.v(TAG, "storage state is " + state);

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            if (requireWriteAccess) {
                boolean writable = checkFsWritable();
                Log.v(TAG, "storage writable is " + writable);
                return writable;
            } else {
                return true;
            }
        } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        r

eturn false;
}

答案 1 :(得分:2)

使用下面的代码

photoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            String filepath = Environment.getExternalStorageDirectory()
                .getPath();
        File file = new File(filepath, "/AudioRecorder/" + image.jpg);

        if (!file.exists()) {
            file.mkdirs();
        }
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(file ));
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });

并在onCreate

之后将其添加到您的代码中
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        Intent i = new Intent(this, Activity2.class);
        startActivity(i);
    }
}