从相机获取图片

时间:2015-05-07 02:36:37

标签: java android io camera

我有一个可以拍照的活动。这样可行。 但是,我不知道如何获得这些图片(不是缩略图)。

我从Android开发者那里阅读文章,但这种方法不起作用。

我的代码:

public class CreateOfferActivity extends ActionBarActivity {

private static final int REQUEST_IMAGE_CAPTURE = 1, REQUEST_TAKE_PHOTO = 1, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private ImageView imgView;
private Button btncreate;
private EditText crttitle, crtdescription, crtprice, crtISBN, crtname, crtemail, crtphone, crttags;
private File picFile;
private Bitmap bit;
private Database1 db;

@Override
protected void onCreate(Bundle savedInstanceState) {
     ...

    imgView = (ImageView) findViewById(R.id.img_view);

    imgView.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            try {
                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                picFile = File.createTempFile("pic_", ""+System.nanoTime(), getCacheDir() );
                i.putExtra(MediaStore.EXTRA_OUTPUT, picFile.getAbsolutePath() );
                startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    setPic();
}
private void saveData(){
    ...
}

private void setPic() {
    if(picFile != null) {
        // Get the dimensions of the View
        int targetW = imgView.getWidth();
        int targetH = imgView.getHeight();

        // Get the dimensions of the bitmap
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile("file:" + picFile.getAbsolutePath(), bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        // Determine how much to scale down the image
        int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

        // Decode the image file into a Bitmap sized to fill the View
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

        bit = BitmapFactory.decodeFile("file:" + picFile.getAbsolutePath(), bmOptions);
        imgView.setImageDrawable(new BitmapDrawable(bit));
    }
}

如果我运行代码,我会得到这个:

Unable to decode stream: java.io.FileNotFoundException: file:/data/data/com.example.leible.appproject/cache/pic_-49734472888666069621067: open failed: ENOENT (No such file or directory)

manifest.xml中的权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

问题可能是捕获的图像没有存储在extra_output路径(picFile)中。请尝试以下链接。 Android ACTION_IMAGE_CAPTURE Intent

修改

它对我有用。

picFile = File.createTempFile("pic_", ""+System.nanoTime(), getExternalCacheDir() );
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(picFile.getAbsolutePath()));

答案 1 :(得分:0)

您可以尝试这样:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 

在您的onActivityResult方法中您可以获得如下图像:

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);
    }  
}