首先導(dǎo)入各種需要的包:
創(chuàng)新互聯(lián)長期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為播州企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、做網(wǎng)站,播州網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
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"));//根據(jù)你實際情況改文件路徑吧
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、得到文件夾中的有圖片的名稱,可以存到數(shù)組或者集合中 3、你再到j(luò)sp頁面做顯示, 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);//創(chuàng)建一個imageView對象??
if?(file.exists())?{??
Bitmap?bm?=?BitmapFactory.decodeFile(filepath);??
//?將圖片顯示到ImageView中??
imageView.setImageBitmap(bm);??
linearLayout1.addView(imageView);??
}??
}?else?{??
textView1.setText("sd卡不存在!");??
}
下面給你提供一個實現(xiàn),該實現(xiàn)采用了代理模式。這個實現(xiàn)包含兩個文件,分別是Client.java和ImageIcoProxy.java,ImageIcoProxy.java負責(zé)了圖片的延遲加載,你可以修改為不延遲即可。
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) {
//已經(jīng)加載了圖片,直接顯示
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();
}
}
);
}
}
}
}