android api里自带了一个Bitmap类,用于处理图片。不是很难,直接上代码

view plaincopy to clipboardprint?
package com.forwork.thumbnail;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
public class Thumbnail extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
saveMyBitmap("ooo");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void saveMyBitmap(String bitName) throws IOException {
File originalFile = new File("sdcard/pic/ll.jpg");
Bitmap bmp = decodeFile(originalFile);
File f = new File("/sdcard/" + bitName + ".jpg");
f.createNewFile();
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//BitmapFactory.Options options=new BitmapFactory.Options();
//options.inSampleSize = 10;
//options.inTempStorage = new byte[16*1024];
//Bitmap bmp = BitmapFactory.decodeFile("/sdcard/pic/sd.jpg");
//Bitmap bmp = BitmapFactory.decodeFile("/sdcard/pic/ll.jpg", options);
//bmp = Bitmap.createScaledBitmap(bmp, 800, 480, true);
bmp.compress(Bitmap.CompressFormat.JPEG, 30, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//The new size we want to scale to
final int REQUIRED_HEIGHT=800;
final int REQUIRED_WIDTH=480;
//Find the correct scale value. It should be the power of 2.
int width_tmp=o.outWidth, height_tmp=o.outHeight;

System.out.println(width_tmp+" "+height_tmp);
Log.w("===", (width_tmp+" "+height_tmp));

int scale=1;
while(true){
if(width_tmp/2<REQUIRED_WIDTH && height_tmp/2<REQUIRED_HEIGHT)
break;
width_tmp/=2;
height_tmp/=2;
scale++;

Log.w("===", scale+"''"+width_tmp+" "+height_tmp);
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
}

更多相关文章

  1. Android中进行HTTP操作
  2. Android上调用google map api v2
  3. Android(安卓)Timer 更好方法
  4. android intent 传递各种结构数据
  5. 【转】 Android上调用google map api v2
  6. Android(安卓)Studio 3.5.1 在项目的build.gradle中添加百分比布
  7. android Java代码的启动
  8. Android(安卓)代码设置来电铃声
  9. Android(安卓)Timer的用法示例

随机推荐

  1. TabLayout用法,android顶部导航栏,android
  2. android 学习第一课
  3. Android使用Animation技巧讲解
  4. android multi user中MTP 多用户的处理
  5. 关于Android(安卓)APK反编译的探索
  6. Google Android开发精华教程
  7. Android手机或Android平板电脑 应用不能
  8. Android中四大组件
  9. 去除Android系统应用的通知功能
  10. android通过USB使用真机调试程序