Android从网络获取图片并放大package irdc.ex08_06;import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.util.Log;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.Matrix;
public class EX08_06 extends Activity
{
private Button mButton1;
private TextView mTextView1;
private ImageView mImageView1;
String uriPic = "http://i.pbase.com/o6/92/229792/1/80199697.uAs58yHk.50pxCross_of_the_Knights_Templar_svg.png";
private static final String tag = "TestImage.............";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); mButton1 = (Button) findViewById(R.id.myButton1);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mImageView1 = (ImageView) findViewById(R.id.myImageView1); mButton1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
Bitmap bitmap =getURLBitmap();
// mImageView1.setImageBitmap(bitmap);
mImageView1.setImageDrawable(resizeImage(bitmap, 300, 300));
mTextView1.setText("");
}
});
} public Bitmap getURLBitmap()
{
URL imageUrl = null;
Bitmap bitmap = null;
try
{
imageUrl = new URL(uriPic);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
try
{
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return bitmap;
}
public static Drawable resizeImage(Bitmap bitmap, int w, int h) { // load the origial Bitmap
Bitmap BitmapOrg = bitmap;
int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();
int newWidth = w;
int newHeight = h;
Log.v(tag,String.valueOf(width));
Log.v(tag,String.valueOf(height));
Log.v(tag,String.valueOf(newWidth));
Log.v(tag,String.valueOf(newHeight)); // calculate the scale
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height; // create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the Bitmap
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45); // recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
height, matrix, true);
return new BitmapDrawable(resizedBitmap);}
}
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.util.Log;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.Matrix;
public class EX08_06 extends Activity
{
private Button mButton1;
private TextView mTextView1;
private ImageView mImageView1;
String uriPic = "http://i.pbase.com/o6/92/229792/1/80199697.uAs58yHk.50pxCross_of_the_Knights_Templar_svg.png";
private static final String tag = "TestImage.............";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); mButton1 = (Button) findViewById(R.id.myButton1);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mImageView1 = (ImageView) findViewById(R.id.myImageView1); mButton1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
Bitmap bitmap =getURLBitmap();
// mImageView1.setImageBitmap(bitmap);
mImageView1.setImageDrawable(resizeImage(bitmap, 300, 300));
mTextView1.setText("");
}
});
} public Bitmap getURLBitmap()
{
URL imageUrl = null;
Bitmap bitmap = null;
try
{
imageUrl = new URL(uriPic);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
try
{
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return bitmap;
}
public static Drawable resizeImage(Bitmap bitmap, int w, int h) { // load the origial Bitmap
Bitmap BitmapOrg = bitmap;
int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();
int newWidth = w;
int newHeight = h;
Log.v(tag,String.valueOf(width));
Log.v(tag,String.valueOf(height));
Log.v(tag,String.valueOf(newWidth));
Log.v(tag,String.valueOf(newHeight)); // calculate the scale
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height; // create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the Bitmap
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45); // recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
height, matrix, true);
return new BitmapDrawable(resizedBitmap);}
}
例如这样的,那你可以只传img//bmp.png这个路径,然后服务器发送这些路径给有这个软件的电脑或者手机,根据这图片的路径去做遍历.....