我的黑莓應用程序應該讀取存儲在SD卡中的Images
。如何在Blackberry模擬器中設置SDCard路徑,以及如何使用FileConnection API從SDCard讀取文件?
我必須在黑莓模擬器中設置SD卡的路徑,以便我可以使用FileConnection API讀取圖像。
任何人都可以給我解決方案嗎?
我的黑莓應用程序應該讀取存儲在SD卡中的Images
。如何在Blackberry模擬器中設置SDCard路徑,以及如何使用FileConnection API從SDCard讀取文件?
我必須在黑莓模擬器中設置SD卡的路徑,以便我可以使用FileConnection API讀取圖像。
任何人都可以給我解決方案嗎?
如果你的意思是你需要設置模擬器路徑爲SD卡,
這裏是你如何在Eclipse中做到這一點的步驟:
1-運行模擬器
2 - 選擇「Smulate」
3-選擇「更改SD卡」
4-按「添加目錄」
5瀏覽並按下「OK」
但如果你需要的代碼在這裏開設的圖片,它是:
FileConnecti onApplication.java:
public class FileConnectionApplication extends UiApplication {
public FileConnectionApplication() {
FileConnectionScreen screen = new FileConnectionScreen();
pushScreen(screen);
}
public static void main(String[] args) {
FileConnectionApplication app = new FileConnectionApplication();
app.enterEventDispatcher();
}
}
FileConnectionScreen.java:
public class FileConnectionScreen extends MainScreen {
private ObjectListField fileList;
private String currentPath = "file:///";
public FileConnectionScreen() {
setTitle("FileConnection");
fileList = new ObjectListField();
fileList.set(new String[] { "store/", "SDCard/" });
add(fileList);
}
protected void makeMenu(Menu menu, int instance) {
super.makeMenu(menu, instance);
menu.add(new MenuItem("Select", 10, 10) {
public void run() {
loadFile();
}
});
}
private void loadFile() {
currentPath += fileList.get(fileList, fileList.getSelectedIndex());
try {
FileConnection fileConnection = (FileConnection) Connector.open(currentPath);
if (fileConnection.isDirectory()) {
Enumeration directoryEnumerator = fileConnection.list();
Vector contentVector = new Vector();
while (directoryEnumerator.hasMoreElements())
contentVector.addElement(directoryEnumerator.nextElement());
String[] directoryContents = new String[contentVector.size()];
contentVector.copyInto(directoryContents);
fileList.set(directoryContents);
} else if (currentPath.toLowerCase().endsWith(".jpg") || currentPath.toLowerCase().endsWith(".png")) {
InputStream inputStream = fileConnection.openInputStream();
byte[] imageBytes = new byte[(int) fileConnection.fileSize()];
inputStream.read(imageBytes);
inputStream.close();
EncodedImage eimg = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
UiApplication.getUiApplication().pushScreen(new ImageDisplayScreen(eimg));
}
} catch (IOException ex) {
}
}
}
ImageDisplayScreen.java:
public class ImageDisplayScreen extends MainScreen {
public ImageDisplayScreen(EncodedImage image) {
int displayWidth = Fixed32.toFP(Display.getWidth());
int imageWidth = Fixed32.toFP(image.getWidth());
int scalingFactor = Fixed32.div(imageWidth, displayWidth);
EncodedImage scaledImage = image.scaleImage32(scalingFactor, scalingFactor);
BitmapField bitmapField = new BitmapField();
bitmapField.setImage(scaledImage);
add(bitmapField);
}
}
1.create folder and give name-SDCard.
2.in the simulator click on-simulate.
3.choose change SD Card.
4.select your folder SDCard.
5.click on close.
now create file connection
FileConnection fileConnection = (FileConnection)Connector.open(("file:///SDCard/images/a.png")
,Connector.READ, true);
InputStream inputStream = fileConnection.openInputStream();
byte[] imageBytes = new byte[(int) fileConnection.fileSize()];
inputStream.read(imageBytes);
inputStream.close();
EncodedImage eimg = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
now u can use this encoded image any where.
你想只設置路徑?或者你也想要一段代碼來讀取SD卡圖像文件? – 2011-04-11 10:10:57