最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程使用Intent傳遞圖片的方法詳解

 更新時間:2017年02月27日 10:28:22   作者:Jacob-wj  
這篇文章主要介紹了Android編程使用Intent傳遞圖片的方法,結合實例形式分析了Android基于Intent傳輸圖片的原理與具體實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了Android編程使用Intent傳遞圖片的方法。分享給大家供大家參考,具體如下:

基本思路是先把bitmap轉(zhuǎn)化為byte數(shù)組,用Intent傳遞數(shù)組,在將數(shù)組轉(zhuǎn)化為bitmap

bitmap轉(zhuǎn)化為byte數(shù)組的方法:

private byte[] Bitmap2Bytes(Bitmap bm){
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
  return baos.toByteArray();
}

byte數(shù)組轉(zhuǎn)化為bitmap方法:

byte buff[]=mIntent.getByteArrayExtra("image");
bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);

程序?qū)嵗?/p>

第一個activity:

import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class SendImageActivity extends Activity implements OnClickListener {
  /** Called when the activity is first created. */
  private Bitmap bitmap;
  byte buff[] = new byte[125*250];
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView mImageView = (ImageView) findViewById(R.id.image);
    Button bt1 = (Button) findViewById(R.id.bt1);
    bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.option24);
    buff = Bitmap2Bytes(bitmap);
    BitmapDrawable mBitmapDrawable = new BitmapDrawable(bitmap);
    mImageView.setBackgroundDrawable(mBitmapDrawable);
    bt1.setOnClickListener(this);
  }
  @Override
  public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent mIntent = new Intent();
    mIntent.putExtra("image", buff);
    mIntent.setClass(this, activity2.class);
    startActivity(mIntent);
  }
  private byte[] Bitmap2Bytes(Bitmap bm){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
    }
}

第二個activity:

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class activity2 extends Activity {
  private Bitmap bitmap;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout2);
    ImageView mImageView = (ImageView) findViewById(R.id.image2);
    Intent mIntent = getIntent();
    byte buff[]=mIntent.getByteArrayExtra("image");
    bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);
    BitmapDrawable mBitmapDrawable = new BitmapDrawable(bitmap);
    mImageView.setBackgroundDrawable(mBitmapDrawable);
  }
}

發(fā)送圖片:

Intent intent = new Intent(ChangePortraitActivity.this , UserProfileActivity.class);
mImageView.setDrawingCacheEnabled(Boolean.TRUE);
intent.putExtra("BITMAP", mImageView.getDrawingCache()); //這里可以放一個bitmap
startActivity(intent);

接收圖片:

//接收的activity
Intent intent = getIntent();
if (intent != null && intent.getParcelableExtra("BITMAP") != null) {
  Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("BITMAP");
  mImageViewPortrait.setImageBitmap(bitmap);
}

更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

相關文章

  • Android中HttpURLConnection類使用介紹

    Android中HttpURLConnection類使用介紹

    早些時候其實我們都習慣性使用HttpClient,但是后來Android6.0之后不再支持HttpClient,需要添加Apache的jar才行,所以,就有很多開發(fā)者放棄使用HttpClient了,HttpURLConnection畢竟是標準Java接口(java.net) ,適配性還是很強的
    2022-12-12
  • Android自定義Toolbar使用方法詳解

    Android自定義Toolbar使用方法詳解

    這篇文章主要為大家詳細介紹了Android自定義Toolbar使用方法 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android端TCP長連接的性能優(yōu)化教程分享

    Android端TCP長連接的性能優(yōu)化教程分享

    在開發(fā)過程中,我們經(jīng)常會用到TCP/IP連接實現(xiàn)即時數(shù)據(jù)傳輸,下面這篇文章主要給大家介紹了關于Android端TCP長連接的性能優(yōu)化的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2018-03-03
  • Android中使用GridView實現(xiàn)仿微信圖片上傳功能(附源代碼)

    Android中使用GridView實現(xiàn)仿微信圖片上傳功能(附源代碼)

    由于工作要求最近在使用GridView完成圖片的批量上傳功能,我的例子當中包含仿微信圖片上傳、拍照、本地選擇、相片裁剪等功能,如果有需要的朋友可以看一下
    2017-08-08
  • Android中Textview超鏈接實現(xiàn)方式

    Android中Textview超鏈接實現(xiàn)方式

    TextView中的超鏈接可以通過幾種方式實現(xiàn):1.Html.fromHtml,2.Spannable,3.Linkify.addLinks。下面分別進行測試,包括修改字體樣式,下劃線樣式,點擊事件等,需要的朋友可以參考下
    2016-02-02
  • android如何設置小區(qū)廣播默認信道(50與60并支持雙卡)

    android如何設置小區(qū)廣播默認信道(50與60并支持雙卡)

    置小區(qū)廣播默認信道50與60,并支持雙卡主要是印度市場,具體的實現(xiàn)如下,感興趣的朋友可以參考下哈
    2013-06-06
  • Android?WindowManager深層理解view繪制實現(xiàn)流程

    Android?WindowManager深層理解view繪制實現(xiàn)流程

    WindowManager是Android中一個重要的Service,是全局且唯一的。WindowManager繼承自ViewManager。WindowManager主要用來管理窗口的一些狀態(tài)、屬性、view增加、刪除、更新、窗口順序、消息收集和處理等
    2022-11-11
  • Android Studio 合并module到統(tǒng)一文件夾的方法

    Android Studio 合并module到統(tǒng)一文件夾的方法

    這篇文章主要介紹了Android Studio 合并module到統(tǒng)一文件夾的方法,補充介紹了android studio關于同名資源文件的合并技巧,需要的朋友可以參考下
    2018-04-04
  • Android實現(xiàn)閱讀APP平移翻頁效果

    Android實現(xiàn)閱讀APP平移翻頁效果

    這篇文章主要介紹了Android實現(xiàn)閱讀APP平移翻頁效果的具體方法,模仿多看閱讀平移翻頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Android framework ATMS啟動流程

    Android framework ATMS啟動流程

    這篇文章主要為大家介紹了Android framework ATMS啟動流程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03

最新評論

遂平县| 靖宇县| 库车县| 彰化县| 临泉县| 扎鲁特旗| 扎囊县| 佛学| 银川市| 屏东市| 郯城县| 湖州市| 吴川市| 昂仁县| 桐梓县| 枣阳市| 黄浦区| 绥德县| 广昌县| 哈尔滨市| 博湖县| 毕节市| 河源市| 阿克陶县| 津南区| 黄龙县| 黄山市| 黔江区| 望都县| 陵水| 建湖县| 田东县| 凌源市| 桂东县| 乐都县| 绥棱县| 临邑县| 思南县| 宁蒗| 五大连池市| 封开县|