简体   繁体   English

从相机获取图片

[英]Get picture of from camera

I have an Activity that can take a picture. 我有一个可以拍照的活动。 That works. 这样可行。 However, I have no clue how I can get these picture (not the thumbnail). 但是,我不知道如何获取这些图片(而不是缩略图)。

I read the articles from Android Developer and this way does not work. 我阅读了Android Developer上的文章,但这种方法行不通。

My Code: 我的代码:

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

If I run the code, I get this: 如果运行代码,则会得到以下信息:

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)

The permissions in the manifest.xml: 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" />

Can someone help me, please? 有人能帮助我吗?

Maybe the problem is that captured image is not stored in extra_output path (picFile). 也许问题在于捕获的图像未存储在extra_output路径(picFile)中。 Try the following link. 尝试以下链接。 Android ACTION_IMAGE_CAPTURE Intent Android ACTION_IMAGE_CAPTURE意向

Edit 编辑

It worked for me. 它为我工作。

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

You can try like this : 您可以这样尝试:

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

And In Your onActivityResult Method You can get the Image like : 并且在您的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);
    }  
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM