展开全部
URL对象中前而几62616964757a686964616fe4b893e5b19e31333363363433个方法都非常容易理解,而该对象提供的openStream()可以读取该 URL资源的InputStream,通过该方法可以非常方便地读取远程资源。
下面的程序示范如何通过URL类读取远程资源:
1)只显示网络图片
1)只显示网络图片
引用图片处理的java类:
package dujun.king.urlgetimage;
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class MainActivity extends Activity {
Bitmap bitmap;
ImageView imageview;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what==0x9527) {
//显示从网上下载的图片
imageview.setImageBitmap(bitmap);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview=(ImageView)findViewById(R.id.imageView1);
//创建并启动一个新线程用于从网络上下载图片
new Thread(){
@Override
public void run() {
try {
//创建一个url对象
URL url=new URL("http://www.baidu.com/img/bdlogo.png");
//打开URL对应的资源输入流
InputStream is= url.openStream();
//从InputStream流中解析出图片
bitmap = BitmapFactory.decodeStream(is);
// imageview.setImageBitmap(bitmap);
//发送消息,通知UI组件显示图片
handler.sendEmptyMessage(0x9527);
//关闭输入流
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
追问
非常感谢您,我想问一下,你这个获得百度logo之后显示,这个实现去除百度logo思路是什么呢,不是很懂呢
![f7a2cb29e94b8461837f2d6a36766535.png](https://img.php1.cn/3cd4a/1eebe/cd5/e62700fe09f8933e.webp)
![7e980dd5f21d69746fc7117fafa2e996.png](https://img.php1.cn/3cd4a/1eebe/cd5/0a0ce631ec450943.webp)
这是我保存到本地的图,多张图,怎么去掉这上面的logo呢
![503e8812ea5945e968b9ad0b72c90052.png](https://img.php1.cn/3cd4a/9b0d/ae9/2d998ad7838fbf16.jpeg)
这是保存代码拜托大神,在后台保存
![303142baadf1f6b8ae987ec7bf102d12.png](https://img.php1.cn/3cd4a/1e618/c5a/d5d40da532c3a782.png)
![34f6d314c491584ebf8695aabfe68a76.png](https://img.php1.cn/3cd4a/1eebe/cd5/a5d7215df572c386.webp)
![bfe7ca5cc3a6a01ac5f2d99472e857c9.png](https://img.php1.cn/3cd4a/1eebe/cd5/0ef126b5295c089b.webp)
![2Q==](https://img.php1.cn/3cd4a/1eebe/cd5/7494af3c1cda418d.webp)
已赞过
已踩过<
你对这个回答的评价是&#xff1f;
评论
收起