1、android 保存图片到数据库android 保存图片到数据库分类:Android2010-04-19 17:264419人阅读评论(2)收藏举报闲话不多说,先看代码 方法一:Java代码1. publicvoidsaveIcon(Bitmapicon) 2. if(icon=null) 3. return; 4. 5. 6. /最终图标要保存到浏览器的内部数据库中,系统程序均保存为SQLite格式,Browser也不例外,因为图片是二进制的所以使用字节数组存储数据库的7. /BLOB类型8. finalByteArrayOutputStreamos=newByteArrayOutputSt
2、ream(); 9. /将Bitmap压缩成PNG编码,质量为100%存储10. press(Bitmap.CompressFormat.PNG,100,os); 11. /构造SQLite的Content对象,这里也可以使用raw12. ContentValuesvalues=newContentValues(); 13. /写入数据库的Browser.BookmarkColumns.TOUCH_ICON字段14. values.put(Browser.BookmarkColumns.TOUCH_ICON,os.toByteArray(); 15. 16. DBUtil.update(.);
3、/调用更新或者插入到数据库的方法17. javaview plaincopy1. publicvoidsaveIcon(Bitmapicon)2. if(icon=null)3. return;4. 5. 6. /最终图标要保存到浏览器的内部数据库中,系统程序均保存为SQLite格式,Browser也不例外,因为图片是二进制的所以使用字节数组存储数据库的7. /BLOB类型8. finalByteArrayOutputStreamos=newByteArrayOutputStream();9. /将Bitmap压缩成PNG编码,质量为100%存储10. press(Bitmap.Compres
4、sFormat.PNG,100,os);11. /构造SQLite的Content对象,这里也可以使用raw12. ContentValuesvalues=newContentValues();13. /写入数据库的Browser.BookmarkColumns.TOUCH_ICON字段14. values.put(Browser.BookmarkColumns.TOUCH_ICON,os.toByteArray();15. 16. DBUtil.update(.);/调用更新或者插入到数据库的方法17. 方法二:如果数据表入口时一个content:URIJava代码1. importandr
5、oid.provider.MediaStore.Images.Media; 2. importandroid.content.ContentValues; 3. importjava.io.OutputStream; 4. 5. /SavethenameanddescriptionofanimageinaContentValuesmap.6. ContentValuesvalues=newContentValues(3); 7. values.put(Media.DISPLAY_NAME,road_trip_1); 8. values.put(Media.DESCRIPTION,Day1,tr
6、iptoLosAngeles); 9. values.put(Media.MIME_TYPE,image/jpeg); 10. 11. /Addanewrecordwithoutthebitmap,butwiththevaluesjustset.12. /insert()returnstheURIofthenewrecord.13. Uriuri=getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,values); 14. 15. /Nowgetahandletothefileforthatrecord,andsavethedatain
7、toit.16. /Here,sourceBitmapisaBitmapobjectrepresentingthefiletosavetothedatabase.17. try 18. OutputStreamoutStream=getContentResolver().openOutputStream(uri); 19. sourceBpress(Bitmap.CompressFormat.JPEG,50,outStream); 20. outStream.close(); 21. catch(Exceptione) 22. Log.e(TAG,exceptionwhilewritingim
8、age,e); 23. 如何在Android当中显示网络图片分类:Android2010-04-19 17:29437人阅读评论(0)收藏举报在android当中显示一张网络图片的时候,其实是比较麻烦的。首先得把这个网络图片转换成java的imputstream流,然后再把这个留转换成一个bitMap.bitMap是可以作为参数传给imageView的。在下边的returnBitMap函数是最核心的,也是大家可以重用的,它负责把一个url的网络图片变成一个本地的BitMap。1. packagecom.jinyan.image;2. 3. importjava.io.IOException;4
9、. importjava.io.InputStream;5. import.HttpURLConnection;6. import.MalformedURLException;7. import.URL;8. 9. importandroid.app.Activity;10. importandroid.graphics.Bitmap;11. importandroid.graphics.BitmapFactory;12. importandroid.os.Bundle;13. importandroid.util.Log;14. importandroid.view.View;15. imp
10、ortandroid.view.View.OnClickListener;16. importandroid.widget.Button;17. importandroid.widget.ImageView;18. 19. publicclassActivityMainextendsActivity20. /*Calledwhentheactivityisfirstcreated.*/21. 22. StringimageUrl=23. BitmapbmImg;24. ImageViewimView;25. 26. Buttonbutton1;27. 28. Override29. publi
11、cvoidonCreate(BundlesavedInstanceState)30. super.onCreate(savedInstanceState);31. setContentView(R.layout.main);32. imView=(ImageView)findViewById(R.id.imview);33. imView.setImageBitmap(returnBitMap(imageUrl);34. 35. 36. 37. 38. publicBitmapreturnBitMap(Stringurl)39. URLmyFileUrl=null;40. Bitmapbitm
12、ap=null;41. try42. myFileUrl=newURL(url);43. catch(MalformedURLExceptione)44. e.printStackTrace();45. 46. try47. HttpURLConnectionconn=(HttpURLConnection)myFileUrl48. .openConnection();49. conn.setDoInput(true);50. conn.connect();51. InputStreamis=conn.getInputStream();52. bitmap=BitmapFactory.decod
13、eStream(is);53. is.close();54. catch(IOExceptione)55. e.printStackTrace();56. 57. returnbitmap;58. 59. 60. 1. xmlversion=1.0encoding=utf-8?2. 5. 7. LinearLayout注意,想要让你的程序可以访问网络,你必须在menifest文件里边增加:1. 完整的menifest文件为:1. 2. 5. 6. 8. 10. 11. 12. 14. 15. 16. 17. android Toast大全(五种情形)建立属于你自己的ToastToast用于向用
14、户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。1.默认效果代码Toast.makeText(getApplicationContext(), 默认Toast样式,Toast.LENGTH_SHORT).show();2.自定义显示位置效果代码toast = Toast.makeText(getApplicationContext(),自定义位置Toast, Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER, 0, 0);toast.show();3.带图片效果代码toast = Toast.
15、makeText(getApplicationContext(),带图片的Toast, Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER, 0, 0);LinearLayouttoastView = (LinearLayout) toast.getView();ImageViewimageCodeProject = new ImageView(getApplicationContext();imageCodeProject.setImageResource(R.drawable.icon);toastView.addView(imageCod
16、eProject, 0);toast.show();4.完全自定义效果代码LayoutInflaterinflater = getLayoutInflater();View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast);ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);image.setImageResource(R.drawable.icon);TextView title = (TextV
17、iew) layout.findViewById(R.id.tvTitleToast);title.setText(Attention);TextView text = (TextView) layout.findViewById(R.id.tvTextToast);text.setText(完全自定义Toast);toast = new Toast(getApplicationContext();toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);toast.setDuration(Toast.LENGTH_LONG);toast.se
18、tView(layout);toast.show();5.其他线程代码new Thread(new Runnable() public void run() showToast();).start();完整代码1.Main,javapackagecom.wjq.toast;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.Gravity;import android.view.LayoutInflater;import android.view.V
19、iew;import android.view.ViewGroup;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;public class Main extends Activity implements OnClickListener Handler handler = new Handler();Overr
20、idepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);findViewById(R.id.btnSimpleToast).setOnClickListener(this);findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(this);findViewById(R.id.btnSimpleToastWithImage).setOn
21、ClickListener(this);findViewById(R.id.btnCustomToast).setOnClickListener(this);findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);public void showToast() handler.post(new Runnable() Overridepublic void run() Toast.makeText(getApplicationContext(), 我来自其他线程!,Toast.LENGTH_SHORT).sho
22、w(););Overridepublic void onClick(View v) Toast toast = null;switch (v.getId() case R.id.btnSimpleToast:Toast.makeText(getApplicationContext(), 默认Toast样式,Toast.LENGTH_SHORT).show();break;case R.id.btnSimpleToastWithCustomPosition:toast = Toast.makeText(getApplicationContext(),自定义位置Toast, Toast.LENGT
23、H_LONG);toast.setGravity(Gravity.CENTER, 0, 0);toast.show();break;case R.id.btnSimpleToastWithImage:toast = Toast.makeText(getApplicationContext(),带图片的Toast, Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER, 0, 0);LinearLayouttoastView = (LinearLayout) toast.getView();ImageViewimageCodeProject = new ImageView(getApplicationContext();imageCodeProject.setImageResource(R.drawable.icon);toastView.addView(image