ImageBrowser

1.效果图

2.代码

2.1MainActivity

[java]  view plain  copy
  1. package com.example.imagebrowser;  
  2.   
  3. import java.io.InputStream;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.MalformedURLException;  
  6. import java.net.URL;  
  7.   
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.os.Message;  
  11. import android.app.Activity;  
  12. import android.graphics.Bitmap;  
  13. import android.graphics.BitmapFactory;  
  14. import android.view.Menu;  
  15. import android.view.View;  
  16. import android.widget.Button;  
  17. import android.widget.EditText;  
  18. import android.widget.ImageView;  
  19.   
  20. public class MainActivity extends Activity  
  21. {  
  22. private EditText et_path;  
  23. private ImageView iv;  
  24. private Button bt;  
  25. private Handler handler=new Handler(){  
  26.     public void handleMessage(android.os.Message msg){  
  27.         Bitmap bitmap=(Bitmap) msg.obj;  
  28.         iv.setImageBitmap(bitmap);  
  29.     };  
  30. };  
  31.     @Override  
  32.     protected void onCreate(Bundle savedInstanceState)  
  33.     {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.activity_main);  
  36.         et_path=(EditText)findViewById(R.id.et_path);  
  37.         iv=(ImageView)findViewById(R.id.iv);  
  38.         bt=(Button) findViewById(R.id.button1);  
  39.     }  
  40. public void click(View v){  
  41.     new Thread(){public void run(){  
  42.         try  
  43.         {  
  44.             String path=et_path.getText().toString().trim();  
  45.             URL url=new URL(path);  
  46.             HttpURLConnection conn=(HttpURLConnection) url.openConnection();  
  47.             conn.setRequestMethod("GET");  
  48.             conn.setConnectTimeout(5000);  
  49.             int code=conn.getResponseCode();  
  50.             if(code==200){  
  51.                 InputStream in=conn.getInputStream();  
  52.                 Bitmap bitmap=BitmapFactory.decodeStream(in);  
  53.                 Message msg=Message.obtain();  
  54.                 msg.obj=bitmap;  
  55.                 handler.sendMessage(msg);  
  56.                           
  57.             }  
  58.         } catch (Exception e)  
  59.         {  
  60.             // TODO Auto-generated catch block  
  61.             e.printStackTrace();  
  62.         }  
  63.     };}.start();  
  64.       
  65. }  
  66.     @Override  
  67.     public boolean onCreateOptionsMenu(Menu menu)  
  68.     {  
  69.         // Inflate the menu; this adds items to the action bar if it is present.  
  70.         getMenuInflater().inflate(R.menu.main, menu);  
  71.         return true;  
  72.     }  
  73.   
  74. }  
2.2 activity_main
[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/et_path"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_above="@+id/button1"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginBottom="48dp"  
  14.         android:ems="10"   
  15.         android:text="https://img.alicdn.com/tps/TB1eREfLVXXXXaHXFXXXXXXXXXX-480-260.png"  
  16.         android:hint="请输入图片的网址">  
  17.   
  18.         <requestFocus />  
  19.     </EditText>  
  20.   
  21.     <Button  
  22.         android:id="@+id/button1"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_alignParentBottom="true"  
  26.         android:layout_centerHorizontal="true"  
  27.         android:onClick="click"  
  28.         android:layout_marginBottom="46dp"  
  29.         android:text="浏览" />  
  30.   
  31.     <ImageView  
  32.         android:id="@+id/iv"  
  33.         android:layout_width="wrap_content"  
  34.         android:layout_height="wrap_content"  
  35.          />  
  36.   
  37. </RelativeLayout>  

2.3增加权限

[html]  view plain  copy
  1. <uses-sdk  
  2.        android:minSdkVersion="14"  
  3.        android:targetSdkVersion="19" />  
  4. lt;uses-permission android:name="android.permission.INTERNET"/> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 图片浏览器可以使用 Java Swing 框架来实现。以下是一个简单的代码示例: ```java import java.awt.BorderLayout; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class ImageBrowser extends JFrame { private JLabel imageLabel; private JButton browseButton; public ImageBrowser() { super("Java Image Browser"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 500); JPanel mainPanel = new JPanel(new BorderLayout()); imageLabel = new JLabel(); mainPanel.add(imageLabel, BorderLayout.CENTER); browseButton = new JButton("Browse"); browseButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory(new File(System.getProperty("user.home"))); int result = fileChooser.showOpenDialog(ImageBrowser.this); if (result == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); displayImage(selectedFile.getAbsolutePath()); } } }); mainPanel.add(browseButton, BorderLayout.SOUTH); getContentPane().add(mainPanel); } private void displayImage(String imagePath) { ImageIcon icon = new ImageIcon(imagePath); Image image = icon.getImage().getScaledInstance(imageLabel.getWidth(), imageLabel.getHeight(), Image.SCALE_SMOOTH); imageLabel.setIcon(new ImageIcon(image)); } public static void main(String[] args) { ImageBrowser imageBrowser = new ImageBrowser(); imageBrowser.setVisible(true); } } ``` 这个程序中,我们使用 `JLabel` 来显示图片,使用 `JButton` 来触发选择文件的操作。当用户选择一个图片文件后,我们将其路径传递给 `displayImage()` 方法,该方法使用 `ImageIcon` 和 `Image` 类来将图片显示在 `JLabel` 上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值