android生成缩略图

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 {

@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得到视频缩略图
  2. 2011.09.27——— android 视频缩略图之MediaMetadataRetriever
  3. Android视频缩略图(二)
  4. Android获取在线视频的缩略图方式对比
  5. android中 MediaStore提取缩略图和原始图像
  6. asp.net实现生成缩略图及加水印的方法示例
  7. 如何通过imagick让PHP生成PSD文件缩略图(实例)
  8. 在上传前预览xls、xlsx、doc等文件的缩略图
  9. 使用python为jpegs创建缩略图

随机推荐

  1. Android整合Maven笔记(一)——在Android项
  2. 使用mount修改你的android中/system为只
  3. Android实战技巧之五十一:libjpeg与Androi
  4. 从头学Android之ContentProvider
  5. Android之adb的使用
  6. Android读取本地json文件的方法(解决显示
  7. IOS/Android 移动端原生及自定义控件开发
  8. android:adb环境变量的配置
  9. Android利用root权限开关机、休眠和唤醒
  10. Android(安卓)颜色渲染(十) ComposeShade