標籤:android activity url下載圖片
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/56/BD/wKiom1SL18DS-xfoAAB0b2g3jvk579.jpg" title="all.jpg" alt="wKiom1SL18DS-xfoAAB0b2g3jvk579.jpg" />
增加了圖片放大縮小功能,一共用到三個類。MainActivity僅作為添加fragment用,其全部代碼如:
package com.example.ex_1213_mypic;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addFragment();//添加片段
}
private void addFragment() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
MyFragment myFragment = new MyFragment();
ft.add(R.id.fl_replace, myFragment);
ft.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
mainActivity添加完fragment就放寒假了。
先在fragment的xml裡添加兩張預設圖片:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/56/BD/wKiom1SL2gfwqftkAABY6LAm_sE175.jpg" title="xml.jpg" alt="wKiom1SL2gfwqftkAABY6LAm_sE175.jpg" />
得到所需圖片地址,並通過AsyncTask執行下載,將下載完的圖片放進聲明出來的bitmapList中,全部下載完成後再初始化控制項:
package com.example.ex_1213_mypic;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
public class MyFragment extends Fragment implements OnClickListener{
String [] urlList = {
"http://7qn7nu.com1.z0.glb.clouddn.com/77.jpg",
"http://7qn7nu.com1.z0.glb.clouddn.com/u=3516622004,3632540994&fm=59.jpg"
};
private View layout;
ArrayList<Bitmap> bitmapList = new ArrayList<Bitmap>();
public MyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(layout == null){
layout = getActivity().getLayoutInflater().inflate(R.layout.fragment_my, null);
initBitmap();//下載圖片
}else{
ViewGroup parent = (ViewGroup) layout.getParent();
parent.removeView(layout);
}
return layout;
}
private int index;
private void initBitmap() {
MyAsync myAsync = new MyAsync();
if(index < 2){
//下載圖片
myAsync.execute(urlList[index]);
}else{
initView();//初始化控制項
}
}
AsyncTask下載過程:
class MyAsync extends AsyncTask<String, Void, Bitmap>{
@Override
protected Bitmap doInBackground(String... params) {
URL url;
Bitmap bitmap = null;
try {
url = new URL(params[0]);
//URLConnection con=url.openConnection();
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.connect();
InputStream is=con.getInputStream();
bitmap=BitmapFactory.decodeStream(is);
is.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
// TODO Auto-generated method stub
super.onPostExecute(bitmap);
if (bitmap != null) {
if(index < 2){
bitmapList.add(bitmap);
index++;
initBitmap();
}
}
}
}
將下載完成的bitmap顯示到控制項中:
private void initView() {
ImageView img1 = (ImageView) layout.findViewById(R.id.imageView1);
ImageView img2 = (ImageView) layout.findViewById(R.id.imageView2);
img1.setImageBitmap(bitmapList.get(0));
img2.setImageBitmap(bitmapList.get(1));
img1.setOnClickListener(this);
img2.setOnClickListener(this);
}
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/56/BB/wKioL1SL3gfjkooIAAKDgPFhYkw007.jpg" title="下載後.jpg" alt="wKioL1SL3gfjkooIAAKDgPFhYkw007.jpg" />
這樣就可以將網路上的圖片正常顯示到APP裡了,記得添加許可權:
<uses-permission android:name="android.permission.INTERNET"/>
最後fragment的圖片傳到MyTouchActivity裡。首先在MyTouchActivity寫一個方法用來接收圖片:
public void getBitmapInfo(Bitmap bitmap, int position){
this.bitmap = bitmap;
this.position = position;
}
執行圖片的點擊事件時,先new 出MyTouchActivity對象並調用上面所寫方法傳入圖片:
//傳出對應的圖片到MytouchActivity,同時從當前activity跳轉到MytouchActivity
private void initClickBitmap(int x) {
Intent intent = new Intent();
MyTouchActivity touchActivity = new MyTouchActivity();
if(bitmapList.size() > x){
touchActivity.getBitmapInfo(bitmapList.get(x), x);
intent.setClass(getActivity(), MyTouchActivity.class);
startActivity(intent);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageView1:
initClickBitmap(0);
break;
case R.id.imageView2:
initClickBitmap(1);
break;
default:
break;
}
}
在MyTouch裡將接收到的bitmap顯示出來就可以了。
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/56/BD/wKiom1SL37Kwa4rcAANtQ80A83Q791.jpg" title="點擊之後.jpg" alt="wKiom1SL37Kwa4rcAANtQ80A83Q791.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/56/BB/wKioL1SL4SLyXctZAAOiNfgpIiU895.jpg" title="手機.jpg" alt="wKioL1SL4SLyXctZAAOiNfgpIiU895.jpg" />
簡單寫一個例子,供有需要的朋友參考,新手上路,不足之處請大俠們海涵並多指教哈~
本文出自 “破公子的偽技術部落格” 部落格,請務必保留此出處http://pogongzi.blog.51cto.com/8982426/1589568
android通過url下載圖片並實現fragment與activity的圖片互動