Created
June 11, 2013 08:24
-
-
Save FeherMarcell/5755264 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.cameraphoto2; | |
import java.io.File; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.provider.MediaStore; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.ImageView; | |
import android.widget.Toast; | |
public class MainActivity extends Activity { | |
Button takePicture; | |
ImageView photo; | |
final String IMAGEPATH = | |
Environment.getExternalStorageDirectory().getAbsolutePath() | |
+ "/tmp_image_new.jpg"; | |
final int REQUEST_TAKE_IMAGE = 1; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
takePicture = (Button) findViewById(R.id.takePicture); | |
photo = (ImageView) findViewById(R.id.photo); | |
takePicture.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View arg0) { | |
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
File file = new File(IMAGEPATH); | |
Uri imageFileUri = Uri.fromFile(file); | |
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri); | |
startActivityForResult(takePhotoIntent, REQUEST_TAKE_IMAGE); | |
} | |
}); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
switch (requestCode) { | |
case REQUEST_TAKE_IMAGE: | |
if(resultCode == Activity.RESULT_CANCELED){ | |
Toast.makeText( | |
getApplicationContext(), | |
"No image was taken!", | |
Toast.LENGTH_SHORT | |
).show(); | |
return; | |
} | |
Bitmap bitmap; | |
BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = true; | |
BitmapFactory.decodeFile(IMAGEPATH, options); | |
int originalWidth = options.outWidth; | |
int originalHeight = options.outHeight; // won't use this now | |
int imageViewWidth = photo.getMeasuredWidth(); | |
int scaleFactor = Math.round(((float)originalWidth / (float)imageViewWidth)); | |
options.inJustDecodeBounds = false; | |
options.inSampleSize = scaleFactor; | |
// get downscaled bitmap | |
bitmap = BitmapFactory.decodeFile(IMAGEPATH, options); | |
// display the bitmap in the ImageView | |
photo.setImageBitmap(bitmap); | |
break; | |
default: | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment