Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Taking photos
with the
camera App
Take a photo
This can be done by easily creating the Intent responsible for the
camera and calling startActivityforResult()
static final int REQUEST_IMAGE_CAPTURE = 1;
Look at the
private void dispatchTakePictureIntent() { intent
Intent i= name!
new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (i.resolveActivity(getPackageManager())
!= null)
{
startActivityForResult(i,
REQUEST_IMAGE_CAPTURE); Remember
} this?
}
Why startActivityforResult and not
startActivity
Getting the thumbnail back!
The Android Camera application encodes the
photo in the return Intent delivered to
onActivityResult() as a small Bitmap in
the extras, under the key "data".
3 Call StartActivityForResult
1 Step 1
String mCurrentPhotoPath; // public variable inside your activity
File storageDir =
Environment.getExternalStoragePu
blicDirectory(
Environment.DIRECTOR
Y_PICTURES);
Create a the file name to be collision free!
mCurrentPhotoPath =
image.getAbsolutePath();
static final int REQUEST_TAKE_PHOTO = 1;
Step 2: create the intent and use the
2
Public void myOnClickMethod(View v) {
previous method
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile(); // we used the method from previous step
} catch (IOException ex) {
// Error occurred while creating the File}
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
You can add the above code inside onActivityResult which means after the photo has
been taken and you came back to your app.
ImageView Scale types
ImageView scales
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/imageView"
android:src="@drawable/leopard"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp" />
android:scaleTypes=
lp.width = 80;
lp.height = 80;
myImageView.setLayoutParams(lp);
One buggy thing!!
When the camera opens, sometimes it
rotates, which might reload your activity.
You know when orientation changes, oncreate
is called again.
To prevent this and prevent crashing, add this
to your <Activity> tag in the manifest file to
tell Android to IGNORE orientation changes.
The screen still orientates, but android wont
take any action.
android:configChanges="orientation|keyboardHidden|screenSize"
SurfaceView
public void adddish(String dname, int nofingred, String dtype, int p, String photopath )
{
SQLiteDatabase db = this.getWritableDatabase();
cv.put(column_dishname,dname);
cv.put(column_nofingredients,nofingred);
cv.put(column_price,p);
cv.put(column_type, dtype);
cv.put(column_photopath,photopath);
Cursor c;
String sqlQuery = "SELECT * FROM " + table_name;
SQLiteDatabase db = this.getReadableDatabase();
c = db.rawQuery(sqlQuery,null);
return c;
}
XMLs
List_item.xml
Linear layout (horizontal) containing an imageView and another Linear Layout (vertical)
that contains the textviews
Custom adapter
public class CustomAdapter extends CursorAdapter {
Dbhelper helper;
Context my_context; // will be used later for getResources functions.
public CustomAdapter(Context con, Cursor cur){
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// grab them from the layout, specifically from the "view" argument.
// grab the values from the cursor. the cursor is the result of some database query.
String n = cursor.getString(cursor.getColumnIndexOrThrow(helper.column_dishname));
String t = cursor.getString(cursor.getColumnIndexOrThrow(helper.column_type));
int i = cursor.getInt(cursor.getColumnIndexOrThrow(helper.column_nofingredients));
int p = cursor.getInt(cursor.getColumnIndexOrThrow(helper.column_price));
int rowid = cursor.getInt(cursor.getColumnIndexOrThrow(helper.column_id));
String path = cursor.getString(cursor.getColumnIndexOrThrow(helper.column_photopath));
Still inside bindView
dname.setText(n);
dtype.setText(t);
nofingred.setText(String.valueOf(i));
price.setText(String.valueOf(p));
iv.setImageBitmap(b); } }}
Add dish activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_dish_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
path = "nophoto";
dname = (EditText) findViewById(R.id.dishname);
dtype = (EditText) findViewById(R.id.dishtype);
dingredients = (EditText) findViewById(R.id.nofingredients);
dprice = (EditText) findViewById(R.id.price);
db = new Dbhelper(this);
imView = (ImageButton) findViewById(R.id.imageView);
Add dish, still inside oncreate
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String n,t;
int i,p;
n = dname.getText().toString();
t = dtype.getText().toString();
i = Integer.parseInt(dingredients.getText().toString());
p = Integer.parseInt(dprice.getText().toString());
db.adddish(n, i, t, p, path);
Toast.makeText(getApplicationContext(),"Dish saved",Toast.LENGTH_SHORT).show();
dname.setText("");
dingredients.setText("");
dprice.setText("");
dtype.setText("");
imView.setImageResource(R.drawable.dishpic);}});
Bonus
Pick images from your gallery
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"),1);
i = (ImageView) findViewById(R.id.myimageView);
lp = i.getLayoutParams();
sb = (SeekBar) findViewById(R.id.seekBar);
sb.setMax(300);
Code cont.
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
lp.width=100+progress;
lp.height=100+progress;
i.setLayoutParams(lp);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});}