4.保存图片 记得加权限
private Uri saveBitmap(Bitmap bm) {
File tmpDir = new File(Environment.getExternalStorageDirectory()
+ "/xiaoxin");
if (!tmpDir.exists()) {
tmpDir.mkdir();
}
File img = new File(tmpDir.getAbsolutePath() + "love.png");
try {
FileOutputStream fos = new FileOutputStream(img);
bm.compress(Bitmap.CompressFormat.PNG, 85, fos);
fos.flush();
fos.close();
Toast.makeText(MainActivity.this, "成功了", Toast.LENGTH_SHORT).show();
return Uri.fromFile(img);
} catch (FileNotFoundException e) {
Toast.makeText(MainActivity.this, "失敗了", Toast.LENGTH_SHORT).show();
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "失敗了", Toast.LENGTH_SHORT).show();
return null;
}
}
5.剪裁图片
/**
* 剪裁图片
*
* @param uri
*/
private void startImageZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_REQUEST_CODE);
}
下面我们再来看一个实例:先是代码的部分,部分是从网路上摘录的,自己整理后当做工具类使用
配置文件:布局很简单,一个ImageButton和一个Button,点击都可以实现图像选择的功能,具体的实现根据大家在实际中用的效果而定
—————————————————————————————————————————————————
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://www.easck.com/apk/res/android"
package="com.cogent.piccut"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".PicCutActivity"
android:screenOrientation="portrait" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>










