简体   繁体   中英

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.

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:

<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). Try the following link. Android ACTION_IMAGE_CAPTURE Intent

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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