首先导入各种需要的包:
创新互联长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为播州企业提供专业的成都网站设计、做网站,播州网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;
读取图片的方法如下:
Image[] array = new Image[10];
Image image = ImageIO.read(new File("d:\\source.gif"));//根据你实际情况改文件路径吧
array[0] = image;
图片读出来了。
如果你有一个Image对象,想把它写入文件可以这样做:
BufferedImage image = ImageIO.read(new File("d:\\source.gif"));
//要想保存这个对象的话你要把image声明为BufferedImage 类型
ImageIO.write(image, "png", new File("f:\\test.png"));
1、获取文件夹的路径 2、得到文件夹中的有图片的名称,可以存到数组或者集合中 3、你再到jsp页面做显示, 4、下面是获取路径和文件名的代码,前台显示的代码自己写 String path = 文件夹路径; String names = ""; try { File f = new File(path)
核心代码
boolean isSdCardExist = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);// 判断sdcard是否存在
if (isSdCardExist) {
String sdpath = Environment.getExternalStorageDirectory()
.getAbsolutePath();// 获取sdcard的根路径
textView1.setText("sd卡是存在的。以下是sdcard下的img25.jpg!");
String filepath = sdpath + File.separator + "img25.jpg";
File file = new File(filepath);
ImageView imageView = new ImageView(this);//创建一个imageView对象
if (file.exists()) {
Bitmap bm = BitmapFactory.decodeFile(filepath);
// 将图片显示到ImageView中
imageView.setImageBitmap(bm);
linearLayout1.addView(imageView);
}
} else {
textView1.setText("sd卡不存在!");
}
下面给你提供一个实现,该实现采用了代理模式。这个实现包含两个文件,分别是Client.java和ImageIcoProxy.java,ImageIcoProxy.java负责了图片的延迟加载,你可以修改为不延迟即可。
Client.java的代码为:
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.Icon;
import javax.swing.JFrame;
public class Client extends JFrame {
private static int IMG_WIDTH = 510;
private static int IMG_HEIGHT = 317;
private Icon imgProxy = null;
public static void main(String[] args) {
Client app = new Client();
app.setVisible(true);
}
public Client() {
super("Virture Proxy Client");
imgProxy = new ImageIcoProxy("D:/test.jpg", IMG_WIDTH, IMG_HEIGHT);
this.setBounds(100, 100, IMG_WIDTH + 10, IMG_HEIGHT + 30);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
Insets insets = getInsets();
imgProxy.paintIcon(this, g, insets.left, insets.top);
}
}
ImageIcoProxy.java的代码为:
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
public class ImageIcoProxy implements Icon {
private ImageIcon realIcon = null;
private String imgName;
private int width;
private int height;
boolean isIconCreated = false;
public ImageIcoProxy(String imgName, int width, int height) {
this.imgName = imgName;
this.width = width;
this.height = height;
}
public int getIconHeight() {
return realIcon.getIconHeight();
}
public int getIconWidth() {
return realIcon.getIconWidth();
}
public void paintIcon(final Component c, Graphics g, int x, int y) {
if (isIconCreated) {
//已经加载了图片,直接显示
realIcon.paintIcon(c, g, x, y);
g.drawString("Just Test", x + 20, y + 370);
} else {
g.drawRect(x, y, width-1, height-1);
g.drawString("Loading photo...", x+20, y+20);
synchronized(this) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Thread.currentThread().sleep(2000);
realIcon = new ImageIcon(imgName);
isIconCreated = true;
} catch (Exception e) {
e.printStackTrace();
}
c.repaint();
}
}
);
}
}
}
}