图片网页浏览器

news/2024/10/17 16:35:06/

图片网页浏览器

首先呢,大家需要在网上找到图片,然后复制图片的网址,记住,是图片网址,而不是当前打开的网页的网址。比如下面这两幅图就分别是我在网页看到的图片,以及用图片浏览器查看时的截图

 

 下面是main_activity.xml代码:

<ImageView

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:layout_weight="1000"

    android:id="@+id/iv" />

    <EditText

        android:singleLine="true"

        android:id="@+id/et_path"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:hint="请输入图片路径"

        />

    <Button

        android:onClick="click"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="浏览"/>

 

 

 

 

</LinearLayout>

 

 下面是MainActivity.Java代码:

public class MainActivity extends Activity {

    protected static final int CHANGE_UI = 1;

    protected static final int ERROR = 2;

    private EditText et_path;

    private ImageView iv;

    private Handler handler = new Handler() {

        public void handleMessage(android.os.Message msg) {

            if (msg.what == CHANGE_UI) {

                System.out.println("I get the picture!");

                Bitmap bitmap = (Bitmap) msg.obj;

                iv.setImageBitmap(bitmap);

            } else if (msg.what == ERROR) {

                Toast.makeText(MainActivity.this, "显示错误图片”Toast.LENGTH_LONG).show();

            }

        }

 

 

        ;

    };

 

 

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        et_path = (EditText) findViewById(R.id.et_path);

        iv = (ImageView) findViewById(R.id.iv);

    }

 

 

    public void click(View view) {

        final String path = et_path.getText().toString().trim();

        if (TextUtils.isEmpty(path)) {

            Toast.makeText(this, "图片路径不能为空", Toast.LENGTH_LONG).show();

        } else {

            new Thread() {

                private HttpURLConnection conn;

                private Bitmap bitmap;

 

 

                public void run() {

                    try {

                        URL url = new URL(path);

                        conn = (HttpURLConnection) url.openConnection();

                        conn.setRequestMethod("GET");

                        conn.setConnectTimeout(5000);

                        conn.setRequestProperty("User-Agent",

                                "Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;" + "SV1;.NET4.0C;.NET4.0E;.NET CLR 2.0.50727;" + ".NET CLR 3.0.4506.2152;" +

                                        ".NET CLR 3.5.30729;Shuame)");

                        int code = conn.getResponseCode();

                        System.out.println(code);

                        if (code == 200) {

                            InputStream is = conn.getInputStream();

                            bitmap = BitmapFactory.decodeStream(is);

                            Message msg = new Message();

                            msg.what=CHANGE_UI;

                            msg.obj = bitmap;

                            handler.sendMessage(msg);

                        } else {

                            Message msg = new Message();

                            msg.what = ERROR;

                            handler.sendMessage(msg);

                        }

                    } catch (MalformedURLException e) {

                        e.printStackTrace();

                        Message msg = new Message();

                        msg.what = ERROR;

                        handler.sendMessage(msg);

                    } catch (ProtocolException e) {

                        e.printStackTrace();

                    } catch (IOException e) {

                        e.printStackTrace();

                    }

                };

            }.start();

        }

    }

 

 

}

 

大家可以参考一下,自己做一下。


http://www.ppmy.cn/news/150200.html

相关文章

图片浏览

图片的浏览&#xff0c;经常会看见在图片的左边点一下转到上一张图片&#xff0c;点右边一下&#xff0c;转到下一张图片&#xff0c;而且鼠标在左边时鼠标是向左的方向&#xff0c;鼠标在右边时鼠标是向右方向。以下示例是用html做的静态的形式&#xff0c;如果用jsp则可以做成…

windows的图片浏览器

windows的图片浏览器 之前用华为手机&#xff0c;截图什么的&#xff0c;windows 7都能正常打开&#xff0c;自从华为手机升级了以后&#xff0c;截图一律都在windows 7的图片浏览器里打不开&#xff0c;提示内存不足&#xff0c;网络上说要用第三方图片软件就可以&#xff0c;…

图片浏览器

ImageViewer 项目地址&#xff1a;albert-lii/ImageViewer 简介&#xff1a; :crystal_ball:图片浏览器&#xff0c;支持图片手势缩放、拖拽等操作&#xff0c;自定义 View的模式显示&#xff0c;自定义图片加载方式&#xff0c;更加灵活&#xff0c;易于扩展&#xff0c;同时…

一个简单的图片浏览

1、图源API 在问度娘之后发现了几个可用的API&#xff0c;主要来自下面几个博主的分享 在线随机美图API接口服务 可以获取随机图片的API收集 随机图片API接口 最终自己测试使用的是下面三个网址&#xff1a; https://uploadbeta.com/api/pictures/random //必应推荐 https://im…

ImageView组件的应用:图片浏览器

随时随地阅读更多技术实战干货&#xff0c;获取项目源码、学习资料&#xff0c;请关注源代码社区公众号(ydmsq666) 本实例用来练习ImageView组件的使用&#xff0c;实现一个图片浏览器&#xff0c;该图片浏览器可以改变所查看的图片的透明度&#xff0c;切换图片&#xff0c;而…

鲁大师2022年Q1季报公布,九号获得最智能电动车品牌

近日&#xff0c;鲁大师2022年Q1季报发布了智能电动车品牌排行榜&#xff0c;其中&#xff0c;备受瞩目的九号电动车品牌以627分的绝对优势夺得鲁大师2022年Q1季度最智能电动车品牌。 据专业人士了解&#xff0c;鲁大师智慧实验室的目标是建立以硬件产品数据采集为主要内容的数…

高德API JS 高德地图获取多个坐标点的中心点

高德API JS 高德地图获取多个坐标点的中心点 一、需求 我需要在地图上展示多个地点&#xff0c;并且展示的同时&#xff0c;地图缩放到合适的大小&#xff0c;要求刚好能显示全部点位&#xff0c;并且边缘留有一部分间隔。 做成如图所示这样。 二、需要用到的 AMap 类库 经…

鲁大师电动车智能化测评报告第九期

鲁大师第九期智能化电动车测评排行榜数据来源于鲁大师智慧实验室&#xff0c;测评的车型均为市面上主流品牌的主流车型&#xff0c;截止目前&#xff0c;鲁大师智能化电动车测评的车型高达40余种&#xff0c;且还在不断增加和丰富中。 一 测评依据 鲁大师电动车智能化测评体系包…