生成圖片文件的方法
(infor from:http://www.pocketdigi.com/20110903/454.html)
Drawable d = main. this . getResources ( ) . getDrawable ( R. drawable . hot ) ; Bitmap bmp = ( ( BitmapDrawable ) d ) . getBitmap ( ) ; //先把Drawable轉成Bitmap,如果是Bitmap,就不用這一步了 FileOutputStream fop ; try { fop = new FileOutputStream ( "/sdcard/test.jpg" ) ; //實例化FileOutputStream,參數是生成路徑 bmp. compress ( Bitmap. CompressFormat . JPEG , 100 , fop ) ; //壓縮bitmap寫進outputStream參數:輸出格式輸出質量目標OutputStream //格式可以為jpg,png,jpg不能存儲透明 fop. close ( ) ; //關閉流 } catch ( FileNotFoundException e ) { // TODO Auto-generated catch block e. printStackTrace ( ) ; } catch ( IOException e ) { // TODO Auto-generated catch block e. printStackTrace ( ) ; }
android文字生成圖片的實現方法(infor from:http://www.eoeandroid.com/thread-536050-1-1.html)
Bitmap bitmap = Bitmap.createBitmap(width, height,Config.ARGB_8888);//創建一個你需要尺寸的Bitmap
Canvas canvas = new Canvas(bitmap);//用這個Bitmap生成一個Canvas,然後canvas就會把內容繪製到上面這個bitmap中
Paint paint = new Paint();//定義一個畫筆
paint.setColor(Color.RED);//紅色畫筆
//往後是關鍵
canvas.drawText(text, x, y , paint);//繪製文案
//繪製Logo
Bitmap logo = xxx;//獲得你的Logobitmap
canvas.drawBitmap(logo, x,y, logo.getWidth(), logo.getHeight());
大致就是這樣,總之就是用canvas.drawXxx方法繪製
Canvas canvas = new Canvas(bitmap);//用這個Bitmap生成一個Canvas,然後canvas就會把內容繪製到上面這個bitmap中
Paint paint = new Paint();//定義一個畫筆
paint.setColor(Color.RED);//紅色畫筆
//往後是關鍵
canvas.drawText(text, x, y , paint);//繪製文案
//繪製Logo
Bitmap logo = xxx;//獲得你的Logobitmap
canvas.drawBitmap(logo, x,y, logo.getWidth(), logo.getHeight());
大致就是這樣,總之就是用canvas.drawXxx方法繪製
android保存Bitmap圖片到指定文件夾示例
(infor from : http://www.jb51.net/article/38480.htm)
/** 保存方法*/
public void saveBitmap() {
Log.e(TAG, "保存圖片");
File f = new File("/sdcard/namecard/", picName);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
Log.i(TAG, "已經保存");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void saveBitmap() {
Log.e(TAG, "保存圖片");
File f = new File("/sdcard/namecard/", picName);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
Log.i(TAG, "已經保存");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在這裡還需要兩個權限:
複製代碼代碼如下:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
沒有留言:
張貼留言