⑴ android,如何讀取資源文件里的圖片到bitmap里
方式:以R文件的方式
路徑:假設 res/drawable下有 test.jpg文件
Bitmap bitmap=BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.test)
⑵ android bitmap 使用時候注意什麼
一、 問題的背景和意義
在Android移動應用開發中,對Bitmap的不小心處理,很容易引起程序內存空間耗盡而導致的程序崩潰問題。比如我們常遇到的問題:
java.lang.OutofMemoryError: bitmap size exceeds VM budget.
導致該問題的出現,一般由以下幾方面原因導致:
引動設備一般存儲空間非常有限。當然不同設備分配給應用的內存空間是不同的。但相對不但提高的設備解析度而言,內存的分配仍然是相對緊張的。
Bitmap對象常常佔用大量的內存空間,比如:對於2592*1936的設備,如果採用ARGB_8888的格式載入圖像,內存佔用將達到19MB空間。
在Anroid App中經常用到ListView,ViewPager等控制項,這些控制項常會包含較大數量的圖片資源。
二、 問題及場景分析
1 高效地載入大圖片。
BitmapFactory類提供了一些載入圖片的方法:decodeByteArray(), decodeFile(), decodeResource(), 等等。
為了避免佔用較大內存,經常使用BitmapFactory.Options 類,設置inJustDecodeBounds屬性為true。
//
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds =true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
為了避免java.lang.OutOfMemory 的異常,我們在真正decode圖片之前檢查它的尺寸,除非你確定這個數據源提供了准確無誤的圖片且不會導致佔用過多的內存。
載入一個按比例縮小的版本到內存中。例如,如果把一個原圖是1024*768 pixel的圖片顯示到ImageView為128*96 pixel的縮略圖就沒有必要把整張圖片都載入到內存中。為了告訴解碼器去載入一個較小的圖片到內存,需要在你的BitmapFactory.Options 中設置 inSampleSize 為true 。例如, 一個解析度為2048x1536 的圖片,如果設置inSampleSize 為4,那麼會產出一個大概為512x384的圖片。載入這張小的圖片僅僅使用大概0.75MB,如果是載入全圖那麼大概要花費12MB(假設bitmap的配置是ARGB_8888).
2 不要在主線程處理圖片。
眾所周知的問題,不再贅述。
注意兩點:1. 為了保證使用的資源能被回收,建議使用WeakReference, 以應用內存內存緊張時,回收部分資源,保證程序進程不被殺死。
2. 避免非同步任務的長時間耗時操作,在任務執行結束後,及時釋放資源。
3 管理Bitmap內存。
在Android開發中,載入一個圖片到界面很容易,但如果一次載入大量圖片就復雜多了。在很多情況下(比如:ListView,GridView或ViewPager),能夠滾動的組件需要載入的圖片幾乎是無限多的。
有些組件的child view在不顯示時會回收,並循環使用,如果沒有任何對bitmap的持久引用的話,垃圾回收器會釋放你載入的bitmap。這沒什麼問題,但當這些圖片再次顯示的時候,要想避免重復處理這些圖片,從而達到載入流暢的效果,就要使用內存緩存和本地緩存了,這些緩存可以讓你快速載入處理過的圖片。
3.1 內存緩存
內存緩存以犧牲內存的代價,帶來快速的圖片訪問。LruCache類(API Level 4之前可以使用Support Library)非常適合圖片緩存任務,在一個LinkedHashMap中保存著對Bitmap的強引用,當緩存數量超過容器容量時,刪除最近最少使用的成員(LRU)。
注意:在過去,非常流行用SoftReference或WeakReference來實現圖片的內存緩存,但現在不再推薦使用這個方法了。因為從Android 2.3 (API Level 9)之後,垃圾回收器會更積極的回收soft/weak的引用,這將導致使用soft/weak引用的緩存幾乎沒有緩存效果。順帶一提,在Android3.0(API Level 11)以前,bitmap是儲存在native 內存中的,所以系統以不可預見的方式來釋放bitmap,這可能會導致短時間超過內存限制從而造成崩潰。 收起
⑶ android 如何實現bitmap分享到QQ空調
public class AndroidShare {
/**
* 上下文
*/
private Context context;
/**
* 文本類型
*
*/
public static int TEXT = 0;
/**
* 圖片類型
*/
public static int DRAWABLE = 1;
public AndroidShare(Context context) {
this.context = context;
}
/**
* 分享到QQ好友
*
* @param msgTitle
* (分享標題)
* @param msgText
* (分享內容)
* @param type
* (分享類型)
* @param drawable
* (分享圖片,若分享類型為AndroidShare.TEXT,則可以為null)
*/
public void shareQQFriend(String msgTitle, String msgText, int type,
Bitmap drawable) {
shareMsg("com.tencent.mobileqq",
"com.tencent.mobileqq.activity.JumpActivity", "QQ", msgTitle,
msgText, type, drawable);
}
/**
* 分享到微信好友
*
* @param msgTitle
* (分享標題)
* @param msgText
* (分享內容)
* @param type
* (分享類型)
* @param drawable
* (分享圖片,若分享類型為AndroidShare.TEXT,則可以為null)
*/
public void shareWeChatFriend(String msgTitle, String msgText, int type,
Bitmap drawable) {
shareMsg("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI", "微信",
msgTitle, msgText, type, drawable);
}
/**
* 分享到微信朋友圈(分享朋友圈一定需要圖片)
*
* @param msgTitle
* (分享標題)
* @param msgText
* (分享內容)
* @param drawable
* (分享圖片)
*/
public void shareWeChatFriendCircle(String msgTitle, String msgText,
Bitmap drawable) {
shareMsg("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI",
"微信", msgTitle, msgText, AndroidShare.DRAWABLE, drawable);
}
/**
* 點擊分享的代碼
*
* @param packageName
* (包名,跳轉的應用的包名)
* @param activityName
* (類名,跳轉的頁面名稱)
* @param appname
* (應用名,跳轉到的應用名稱)
* @param msgTitle
* (標題)
* @param msgText
* (內容)
* @param type
* (發送類型:text or pic 微信朋友圈只支持pic)
*/
@SuppressLint("NewApi")
private void shareMsg(String packageName, String activityName,
String appname, String msgTitle, String msgText, int type,
Bitmap drawable) {
if (!packageName.isEmpty() && !isAvilible(context, packageName)) {// 判斷APP是否存在
Toast.makeText(context, "請先安裝" + appname, Toast.LENGTH_SHORT)
.show();
return;
}
Intent intent = new Intent("android.intent.action.SEND");
if (type == AndroidShare.TEXT) {
intent.setType("text/plain");
} else if (type == AndroidShare.DRAWABLE) {
intent.setType("image/*");
// BitmapDrawable bd = (BitmapDrawable) drawable;
// Bitmap bt = bd.getBitmap();
final Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(
context.getContentResolver(), drawable, null, null));
intent.putExtra(Intent.EXTRA_STREAM, uri);
}
intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
intent.putExtra(Intent.EXTRA_TEXT, msgText);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (!packageName.isEmpty()) {
intent.setComponent(new ComponentName(packageName, activityName));
context.startActivity(intent);
} else {
context.startActivity(Intent.createChooser(intent, msgTitle));
}
}
/**
* 判斷相對應的APP是否存在
*
* @param context
* @param packageName
* @return
*/
public boolean isAvilible(Context context, String packageName) {
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
for (int i = 0; i < pinfo.size(); i++) {
if (((PackageInfo) pinfo.get(i)).packageName
.equalsIgnoreCase(packageName))
return true;
}
return false;
}
/**
* 指定分享到qq
* @param context
* @param bitmap
*/
public void sharedQQ(Activity context,Bitmap bitmap){
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(
context.getContentResolver(), BitmapFactory.decodeResource(context.getResources(), R.mipmap.company_mainback), null, null));
Intent imageIntent = new Intent(Intent.ACTION_SEND);
imageIntent.setPackage("com.tencent.mobileqq");
imageIntent.setType("image/*");
imageIntent.putExtra(Intent.EXTRA_STREAM, uri);
imageIntent.putExtra(Intent.EXTRA_TEXT,"您的好友邀請您進入天好圈");
imageIntent.putExtra(Intent.EXTRA_TITLE,"天好圈");
context.startActivity(imageIntent);
}
}
⑷ android開發怎麼得到Bitmap所佔資源的大小
首先得到Bitmap對象所佔資源的大小,在新的API上提供了一個方法
bitmap.getByteCount() // from API Level 12
也就是說從SDK12才能使用這個方法,針對以前的版本還是不能使用,那麼怎麼辦?看第二種方法
bitmap.getRowBytes() * bitmap.getHeight() //這樣也能很准確的計算出Bitmap所佔內存的大小,方法都是從SDK1就開始存在的。bingo!正解!
需要注意的是我上面說的兩種方法是得到bitmap對象在內存中所佔的存儲空間大小,其實比實際圖片(比如圖片文件)大,如果想得到文件大小呢?
如何得到bitmap所使用圖片的文件大小?
bitmap.compress(format, quality, stream)
至於方法的解釋,參數的傳入自己去看API文檔,最後一個參數是一個OutPutStream對象,得到大小
⑸ android 怎麼上傳bitmap 到資料庫
andorid推薦的是如果數據在20-50k可以存放到資料庫,否則資料庫存路徑。
⑹ android bundle能傳遞bitmap嗎
可以啊 可以直接傳好吧!
Bitmap bitmap = null;
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", bitmap);
intent.putExtra("bundle", bundle);
bitmap已經自己實現了Parcelable介面
⑺ android怎麼生成bitmap
1、
[java] view plain
public Bitmap convertViewToBitmap(View view){
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
Bitmap.Config.ARGB_8888);
//利用bitmap生成畫布
Canvas canvas = new Canvas(bitmap);
//把view中的內容繪制在畫布上
view.draw(canvas);
return bitmap;
}
2、
[java] view plain
/**
* save view as a bitmap
*/
private Bitmap saveViewBitmap(View view) {
// get current view bitmap
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bitmap = view.getDrawingCache(true);
Bitmap bmp = plicateBitmap(bitmap);
if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); bitmap = null; }
// clear the cache
view.setDrawingCacheEnabled(false);
return bmp;
}
public static Bitmap plicateBitmap(Bitmap bmpSrc)
{
if (null == bmpSrc)
{ return null; }
int bmpSrcWidth = bmpSrc.getWidth();
int bmpSrcHeight = bmpSrc.getHeight();
Bitmap bmpDest = Bitmap.createBitmap(bmpSrcWidth, bmpSrcHeight, Config.ARGB_8888); if (null != bmpDest) { Canvas canvas = new Canvas(bmpDest); final Rect rect = new Rect(0, 0, bmpSrcWidth, bmpSrcHeight);
canvas.drawBitmap(bmpSrc, rect, rect, null); }
return bmpDest;
}
⑻ android 怎麼獲得bitmap對象
保存界面可視化組件,在調用View.getDrawingCache方法前需要調用measure和layout方法才能可以成功獲取布局的Bitmap對象
代碼如下:
//獲得xml的布局截圖
View view=getLayoutInflate r().inflate(R.layout.dialog, null);
//打開圖像緩存
view.setDrawingCacheEnabled(true);
//需要調用measure和layout方法
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//發送位置和尺寸到view及其所有的子view
view.layout(0, 0, view.getMeasuredWidth(),view.getMeasuredHeight());
//獲得可視化組件的截圖
Bitmap bitmap=view.getDrawingCache();
try {
//保存在sdcard目錄下
FileOutputStream fos=new FileOutputStream("/sdcard/test.png");
bitmap.compress(CompressFormat.PNG, 100, fos);
//關閉文件輸出流
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
⑼ android 讀取Bitmap的幾種方式
想讀取本地項目里的資源圖片,但又不能用到R文件。查了很多資料終於找到了。 現總結以下幾種讀取Bitmap的方法。 1.以文件流的方式,假設在sdcard下有 test.png圖片 FileInputStream fis = new FileInputStream("/sdcard/test.png"); Bitmap bitmap=BitmapFactory.decodeStream(fis); 2. 以R文件的方式,假設 res/drawable下有 test.jpg文件Bitmapbitmap =BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.test); 3.以ResourceStream的方式,但不用到R文件。 Bitmap.bitmap=BitmapFactory.decodeStream(getClass().getResourceAsStream(「/res/drawable/test.png」)); 圖片名就可以讀取到 Bitmap啦。 BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; //圖片寬高都為原來的二分之一,即圖片為原來的四分一 //以上代碼可以優化內存溢出,但它只是改變圖片大小,並不能徹底解決內存溢出。
⑽ android 自帶的分享功能如何實現分享圖片
bgimg0=getImageFromAssetsFile("Cat_Blink/cat_blink0000.png");
/**
*從Assets中讀取圖片
*/
(StringfileName)
{
Bitmapimage=null;
AssetManageram=getResources().getAssets();
try
{
InputStreamis=am.open(fileName);
image=BitmapFactory.decodeStream(is);
is.close();
}
catch(IOExceptione)
{
e.printStackTrace();
}
returnimage;
}
上面的代碼是從assets中獲取圖片的代碼,下面的代碼是分享圖片的代碼:
/**
*分享功能
*
*@paramcontext
*上下文
*@paramactivityTitle
*Activity的名字
*@parammsgTitle
*消息標題
*@parammsgText
*消息內容
*@paramimgPath
*圖片路徑,不分享圖片則傳null
*/
publicvoidshareMsg(StringactivityTitle,StringmsgTitle,StringmsgText,
StringimgPath){
Intentintent=newIntent(Intent.ACTION_SEND);
if(imgPath==null||imgPath.equals("")){
intent.setType("text/plain");//純文本
}else{
Filef=newFile(imgPath);
if(f!=null&&f.exists()&&f.isFile()){
intent.setType("image/jpg");
Uriu=Uri.fromFile(f);
intent.putExtra(Intent.EXTRA_STREAM,u);
}
}
intent.putExtra(Intent.EXTRA_SUBJECT,msgTitle);
intent.putExtra(Intent.EXTRA_TEXT,msgText);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent,activityTitle));
}
系統的分享,你想要分享圖片需要先把圖片存到本地才能分享