android多媒体图片mediaStore
界面首先由一个choosePicture的按钮,用来从MediaStore取得图片,取得之后,可以在图片上进行绘画,再点击savebutton就可以保存图片到MediaStore。
界面xml文件:
[html] view plaincopyprint?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/chooseButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/choose" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical|center"
android:contentDescription="@string/contentDes" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="right|bottom"
android:text="@string/save" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/chooseButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/choose" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical|center" android:contentDescription="@string/contentDes" /> <Button android:id="@+id/saveButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_gravity="right|bottom" android:text="@string/save" /> </LinearLayout>
string.xml文件定义使用的一些字符串:
[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Scrawl</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="choose">选择图片</string>
<string name="contentDes">图片描述</string>
<string name="save">保存图片</string>
<string name="pleaseChooseImage">请选择图片</string>
<string name="saveSuccess">保存图片成功</string>
</resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Scrawl</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="choose">选择图片</string> <string name="contentDes">图片描述</string> <string name="save">保存图片</string> <string name="pleaseChooseImage">请选择图片</string> <string name="saveSuccess">保存图片成功</string> </resources>
在dimens.xml中定义了margin的top和bottom值为16dp:
[html] view plaincopyprint?
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources>
主程序:
[java] view plaincopyprint?
package cn.yh.scrawl;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener,
OnTouchListener {
private static final int CHOOSEPICTURE_REQUESTCODE = 0;
private static final String TAG = "Scrawl";
// �@示�D片的�M件。
private ImageView imageView;
// �x��D片的按�o,和保存�D片的按�o。
private Button chooseButton, saveButton;
// 定�x���位�D�ο螅�第一��包含了�x�穸�D片的�s放版本,第二��是可�的版本,
// �⒌谝��位�D�ο罄L�u到第二��位�D�ο笾校�再在其上方�L�u(�T�f)
private Bitmap bitmap, alteredBitmap;
// 定�x��布
private Canvas canvas;
// 定�x���P
private Paint paint;
// 定�x�入矩�,��使之在一幅�D像上��用空�g�D�Q(比如旋�D,�u�h,�s放,裁剪等)
private Matrix matrix;
// 定�x按下和停止的位置(x,y)座��
private float downX = 0, downY = 0, upX = 0, upY = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// �O置主��面
setContentView(R.layout.activity_main);
// �@取��面xml文件相�P的View�ο�
imageView = (ImageView) findViewById(R.id.imageView);
chooseButton = (Button) findViewById(R.id.chooseButton);
saveButton = (Button) findViewById(R.id.saveButton);
// �o���Button�O置onClick�O�事件,�����F了android.view.View.OnClickListener接口
chooseButton.setOnClickListener(this);
saveButton.setOnClickListener(this);
// �oImageView�ο笤O置onTouch�O�,�����F了android.view.View.OnTouchListener接口
imageView.setOnTouchListener(this);
// ��_始,�O置saveButton�椴豢捎��B
saveButton.setEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 因�榻o不同的Button�ο蠖荚O置了onClick�O�,所以需要判�嘤|�l的是哪��Button的�O�事件
switch (v.getId()) {
case R.id.chooseButton:
// 再一次选择图片,�O置saveButton�椴豢捎��B
if (saveButton.isEnabled())
saveButton.setEnabled(false);
// 使用Intent打�_Gallery�x��D片
Intent choosePictureIntent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// ��釉�activity,并在�activity�Y束返回���,所以�{用startActivityForResult()方法
startActivityForResult(choosePictureIntent,
CHOOSEPICTURE_REQUESTCODE);
break;
case R.id.saveButton:
if (alteredBitmap != null) {
Uri imageFileUri = getContentResolver().insert(
Media.EXTERNAL_CONTENT_URI, new ContentValues());
try {
OutputStream imageFileOS = getContentResolver()
.openOutputStream(imageFileUri);
//compress方法,压缩成jpg格式,0-100代表了压缩质量 100质量最好 第三个参数把压缩图片写到输出流
alteredBitmap
.compress(CompressFormat.JPEG, 90, imageFileOS);
//保存成功,进行提示
Toast.makeText(this, R.string.saveSuccess,
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(this, R.string.pleaseChooseImage,
Toast.LENGTH_LONG).show();
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
// 判�喾祷厥欠�OK
if (resultCode == RESULT_OK) {
// �@取返回的Uri
Uri imageFileUri = data.getData();
// �@取默�JDisplay,用以得到��前的��和高
Display currentDisplay = getWindowManager().getDefaultDisplay();
int dw = currentDisplay.getWidth();
Log.i(TAG, currentDisplay.getHeight()+"");
Log.i(TAG, chooseButton.getHeight()+"chooseButton");
Log.i(TAG, saveButton.getHeight()+"saveButton");
Log.i(TAG, imageView.getHeight()+"imageView");
Log.i(TAG, getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop()+"ID_ANDROID_CONTENT");
//432 = 48+48+40+16*2+264 默认界面的margin top bottom有16dp title和状态栏40dp 48+48 两个button高度
int dh = currentDisplay.getHeight() - (48+48+40+16*2);
// chooseButton.getHeight()
// - saveButton.getHeight();
// 使用BitmapFactory��建位�DOptions
BitmapFactory.Options bmFactoryOptions = new Options();
// �O置只是�@取�D片的尺寸,�K不是真正的解�a�D片
bmFactoryOptions.inJustDecodeBounds = true;
// 使用decode*方法��建位�D,getContentResolver()�@取�热萏峁┢�
try {
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageFileUri), null, bmFactoryOptions);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// �算�D像��高�c��前��高的比率
int hRatio = (int) Math.ceil(bmFactoryOptions.outHeight
/ (float) dh);
int wRatio = (int) Math
.ceil(bmFactoryOptions.outWidth / (float) dw);
// 判断是按高比率缩放还是宽比例缩放
if (hRatio > 1 || wRatio > 1) {
if (hRatio > wRatio) {
bmFactoryOptions.inSampleSize = hRatio;
} else {
bmFactoryOptions.inSampleSize = wRatio;
}
}
Log.i(TAG, bmFactoryOptions.outHeight/hRatio+"imageView");
// 对图像进行真正的解码
bmFactoryOptions.inJustDecodeBounds = false;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageFileUri), null, bmFactoryOptions);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 在加�d位�D之後,��建一��可�的位�D�ο�alteredBitmap,�K在其中�L�ubitmap�ο�
alteredBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), bitmap.getConfig());
// 使用alteredBitmap作���造��担���建canvas
canvas = new Canvas(alteredBitmap);
// ��建���P
paint = new Paint();
// �O置���P�色��Color.WHITE
paint.setColor(Color.WHITE);
// �O置���P大小
paint.setStrokeWidth(5);
// ��建matrix,此���D像不�M行任何的�s放,旋�D等操作
matrix = new Matrix();
// �L�ubitmap
canvas.drawBitmap(bitmap, matrix, paint);
// 把alteredBitmap�O置到imageview上面
imageView.setImageBitmap(alteredBitmap);
// 此�r�O置saveButton�榭捎��B
saveButton.setEnabled(true);
imageView.setOnClickListener(this);
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
// 判�嗖煌���B
switch (action) {
case MotionEvent.ACTION_DOWN:
// 按下�r��座��
downX = event.getX();
downY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
// 移�舆^程中,不�嗬L�uLine
upX = event.getX();
upY = event.getY();
canvas.drawLine(downX, downY, upX, upY, paint);
imageView.invalidate();
downX = upX;
downY = upY;
break;
case MotionEvent.ACTION_UP:
// 停止�r,��座��
upX = event.getX();
upY = event.getY();
canvas.drawLine(downX, downY, upX, upY, paint);
imageView.invalidate();
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
//返回true表示,一旦事件开始就要继续接受触摸事件
return true;
}
}
转载网址:http://blog.csdn.net/yhcelebrite/article/details/11786853