Android Camera Activity会生成重复的照片

时间:2012-09-05 18:41:04

标签: android photo

我使用以下代码获取存储在SD卡文件夹中的照片。这工作正常。但照片也存储在SD卡图像文件夹中,并显示在机器人照片库中。为什么呢?

提前感谢!

public class Foto extends Activity implements SurfaceHolder.Callback,
    OnClickListener {

private static final int CAMERA_ACTIVITY = 1;
private static final int TAKE_PICTURE = 1;
File archive, root, folder;
String fec;

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String storageState = Environment.getExternalStorageState();
    if (storageState.equals(Environment.MEDIA_MOUNTED)) {

        String path = Environment.getExternalStorageDirectory().getName()
                + File.separatorChar + "TEST" + "/"
                + "test.jpg";
        archive = new File(path);
        try {
            if (archive.exists() == false) {
                archive.getParentFile().mkdirs();
                archive.createNewFile();
            }
            if (archive.exists() == true) {

                archive.createNewFile();
            }

        } catch (IOException e) {
            Log.e(ACTIVITY_SERVICE, "Could not create file.", e);
        }
        Log.i(ACTIVITY_SERVICE, path);

        Uri _fileUri = Uri.fromFile(archive);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, _fileUri);
        startActivityForResult(intent, TAKE_PICTURE);


        setResult(RESULT_OK, intent);
        finish();
    } else {
        new AlertDialog.Builder(Foto.this)
                .setMessage(
                        "External Storeage (SD Card) is required.\n\nCurrent state: "
                                + storageState).setCancelable(true)
                .create().show();

        finish();
    }

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:1)

在大多数设备上,MediaStore.ACTION_IMAGE_CAPTURE除了执行任何其他操作外,还会保存到Gallery中,即使您没有要求返回文件,也只是URI。我知道在Nexus 7上它并没有这样做,但这真的很糟糕,这种行为只是神奇地不同而且很烦人。无论如何,还要注意的是,如果媒体扫描程序已经赶上,画廊实际上会显示SD卡上任何地方没有.nomedia文件的任何内容。