We will make an app for
entertainment, when user open phone camera, a ghost or any special thing you
want appear. Take picture and save internal storage.
This is special creature we
use.
Create a new project, in file
xml we use FrameLayout to overlap two imageViews.
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/l" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="400dp"
android:scaleType="fitCenter"
/>
<ImageView
android:id="@+id/image2"
android:layout_width="100dp"
android:layout_height="138dp"
android:layout_marginBottom="1dp"
android:layout_gravity="center"
android:contentDescription="@null"
/>
</FrameLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
>
<Button
android:id="@+id/bu1"
android:layout_width="80dp"
android:layout_height="40dp"
android:background="@drawable/back2"
android:text="Take picture"
android:textColor="#0000cd"
android:textSize="2mm"
/>
<Button
android:id="@+id/bu2"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_marginLeft="4dp"
android:background="@drawable/back2"
android:text="Save"
android:textColor="#0000cd"
android:textSize="14sp"
/>
</LinearLayout>
</LinearLayout>
File back2.xml to make round
corner button like this.
<?xml version="1.0"
encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:state_pressed="true"
>
<shape android:shape="rectangle" >
<corners android:radius="20dip"
/>
<stroke android:width="0.5dip"
android:color="#6699cc" />
<gradient android:angle="-90"
android:startColor="#66ff00" android:endColor="#bcd4e6" />
</shape>
</item>
<item android:state_focused="true">
<shape
android:shape="rectangle" >
<corners
android:radius="20dip" />
<stroke android:width="0.5dip"
android:color="#6699cc" />
<solid android:color="#afeeee"/>
</shape>
</item>
<item >
<shape
android:shape="rectangle" >
<corners
android:radius="20dip" />
<stroke
android:width="0.5dip" android:color="#6699cc"
/>
<gradient android:angle="-90"
android:startColor="#E6E6FA" android:endColor="#e6e8fa"
/>
</shape>
</item>
</selector>
In Main class, declare
variables.
Button b1,b2;
ImageView im,im2;
File des;
FrameLayout layout;
Add address.
b1 =
(Button)findViewById(R.id.bu1);
b2 =
(Button)findViewById(R.id.bu2);
layout =
(FrameLayout)findViewById(R.id.l);
im =
(ImageView)findViewById(R.id.image);
im2 =
(ImageView)findViewById(R.id.image2);
des = new
File(Environment
.getExternalStorageDirectory(),
"image.jpg");
In button Take picture we turn
camera on and startActivityForResult to wait for picture
result.
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(des));
startActivityForResult(intent,
1);
} catch
(ActivityNotFoundException e) {
//Handle if no application
exists
}
In Override command, we receive
picture, set to imageView.
Copy to above last close
bracket.
@Override
protected void
onActivityResult(int requestCode,
int resultCode,
Intent data) {
if(requestCode == 1
&& resultCode ==
Activity.RESULT_OK) {
try {
FileInputStream in =
new
FileInputStream(des);
BitmapFactory.Options options =
new
BitmapFactory.Options();
options.inSampleSize = 10;
Bitmap userImage = BitmapFactory
.decodeStream(in,
null, options);
im.setImageBitmap(userImage);
im2.setBackgroundResource(R.drawable.ghost);
} catch (Exception e) {
e.printStackTrace();
}
}
}
To save picture, create new
class name save, add
this to Androidmanifest.
<activity
android:name="com.bai.baidau.save"
android:windowSoftInputMode="stateAlwaysVisible" android:theme="@android:style/Theme.Holo.Light.Dialog.NoActionBar"
android:screenOrientation="portrait">
</activity>
In class code, we declare
EditText, Button, in button OK, we check Edittext, if no character, show Toast,
else, send String picture name to Main class.
String name = e.getText().toString();
if (ten == null || name.trim().equals("")) {
Toast.makeText(luu.this, "Enter
picture name!",
Toast.LENGTH_SHORT).show();
}
else {
Intent
in = getIntent();
Bundle
bun = new Bundle();
bun.putString("iz", name);
in.putExtras(bun);
setResult(4,
in);
finish();
}
In Cancel button, we quit class
by command finish().
Back to class Main, button Save
picture, we open class save, wait for picture send back.
Intent in=new
Intent(getBaseContext(),save.class);
startActivityForResult(in,
1);
In Override, add these lines.
if(resultCode ==
4){
Bundle bun = data.getExtras();
String ve = bun.getString("iz");
String picname=ve+".jpg";
Toast.makeText(image.this, "Picture save!", Toast.LENGTH_SHORT).show();
File em =
new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES),picname);
layout.setDrawingCacheEnabled(true);
layout.setDrawingCacheQuality(LinearLayout.DRAWING_CACHE_QUALITY_HIGH);
layout.buildDrawingCache();
Bitmap
b = layout.getDrawingCache();
try {
FileOutputStream fos = new
FileOutputStream(em);
b.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch
(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
layout.destroyDrawingCache();
}
We get picture, show infrom
saved, save to Pictures Directory of phone, destroy layout to prevent coincide
when user take new picture.
Last, add permission in to file
AndoidManifest.
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
The picture we get will look
like this.
No comments:
Post a Comment