使用getExternalStoragePublicDirectory保存图像

时间:2014-02-04 01:51:54

标签: android android-intent android-activity mediastore

我正在尝试编写一个允许用户捕获图像的Android应用程序。用户将按下" Capture"当按钮准备拍摄图像时,相机将打开以供他们拍照,然后照片将保存到DCIM\testdir

到目前为止,我可以让相机打开并拍照,但拍完照片后,我无法正确保存(见图片):http://i.imgur.com/4IkFowo.png

我可以点击取消按钮带我回到我的应用程序,我也可以点击刷新按钮拍摄另一张照片,但按下检查按钮不会做任何事情。我想将它保存到我指定的目录并将我带回我的应用程序。

这是我的源代码:

import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ContactSelected extends Activity {
    private Uri fileUri;
    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_selected);

        Button cameraButton = (Button) findViewById(R.id.bLaunchCamera); 
        cameraButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Do something when a list item is clicked

                // Create Intent to take a picture and return control to the calling application
                Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File imagesFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "testdir");
                imagesFolder.mkdirs();
                imagesFolder.setWritable(true);
                File image = new File(imagesFolder, "image_001.jpg");
                fileUri = Uri.fromFile(image);
                camera.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name

                // Start the image capture Intent
                startActivityForResult(camera, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            }

            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
                    if (resultCode == RESULT_OK) {
                        finish();
                    }
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.contact_selected, menu);
        return true;
    }

}

0 个答案:

没有答案