android通过url下载图片并实现fragment与activity的图片交互



        

    增加了图片放大缩小功能,一共用到三个类。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里添加两张默认图片:

    


    得到所需图片地址,并通过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);

}


    



    这样就可以将网络上的图片正常显示到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显示出来就可以了。


    


    


    简单写一个例子,供有需要的朋友参考,新手上路,不足之处请大侠们海涵并多指教哈~

    


    


本文出自 “破公子的伪技术博客” 博客,请务必保留此出处http://pogongzi.blog.51cto.com/8982426/1589568

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。