Php 为什么从Web服务器下载照片后会引发NullPointerException?
我正在从Web服务器下载一张照片。以下是在网页url中显示照片的PHP代码:Php 为什么从Web服务器下载照片后会引发NullPointerException?,php,java-me,lwuit,Php,Java Me,Lwuit,我正在从Web服务器下载一张照片。以下是在网页url中显示照片的PHP代码: <?php $rep = $_GET['dossierClient']; if (file_exists($rep)) { $myDirectory = opendir($rep); while($entryName = readdir($myDirectory)) { $dirArray[] = $entryName; }
<?php
$rep = $_GET['dossierClient'];
if (file_exists($rep))
{
$myDirectory = opendir($rep);
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}
closedir($myDirectory);
$indexCount = count($dirArray);
sort($dirArray);
for($index=0; $index < $indexCount; $index++)
{
if (substr("$dirArray[$index]", 0, 1) != ".")
{
print("<img src=$rep/$dirArray[$index] />");
//print("\n");
}
}
}
?>
这里的图像引用了LWUIT图像。
downloadImage方法的代码为:
private Image downloadImage(String url) throws IOException
{
Image img = null;
byte[] rawImg = null;
try
{
String imageData = getDataFromUrl(url);
rawImg = imageData.getBytes();
putPhotoToPhone(rawImg);
img = Image.createImage(rawImg, 0, rawImg.length );
}
catch(Exception e1) {
e1.printStackTrace();
}
return img;
}
public String getDataFromUrl(String url) throws IOException {
StringBuffer b = new StringBuffer();
InputStream is = null;
HttpConnection c = null;
long len = 0 ;
int ch = 0;
c = (HttpConnection)Connector.open(url);
is = c.openInputStream();
len = c.getLength();
if( len != -1)
{
for(int i =0 ; i < len ; i++ )
{
if((ch = is.read()) != -1)
{
b.append((char) ch);
}
}
}
else
{
while ((ch = is.read()) != -1)
{
len = is.available() ;
b.append((char)ch);
}
}
is.close();
c.close();
return b.toString();
}
private void putPhotoToPhone(byte[] rawImg)
{
FileConnection fcDir, fcFile;
int photoId, photoNextCounter;
String fileName;
OutputStream os;
if (rawImg != null)
{
try {
fcDir = (FileConnection) Connector.open("file:///"+pRoot+photoDirectory+"/", Connector.READ_WRITE);
if(!fcDir.exists())
fcDir.mkdir();
if (vPhotosName.isEmpty())
photoNextCounter = 1;
else
photoNextCounter = 1;
//photoNextCounter = getNextImageCounter(fcDir, String.valueOf(backForm.vPhotosName.elementAt(backForm.vPhotosName.size()-1)));
fileName = "photo_downloaded_" + photoNextCounter + ".png";
fcFile = (FileConnection) Connector.open("file:///"+pRoot+photoDirectory+"/"+fileName, Connector.READ_WRITE);
if(!fcFile.exists())
fcFile.create();
os = fcFile.openOutputStream();
os.write(rawImg);
os.close();
fcFile.close();
fcDir.close();
try {
photoId = rsImage.addRecord(rawImg, 0, rawImg.length);
vRawPhotoIDs.addElement(new Integer(photoId));
}
catch (RecordStoreException ex) {}
vPhotosName.addElement(fileName);
}
catch (Exception e) {}
}
}
那么如何正确下载呢?最有可能的情况是,您的图像无法下载,从而导致在调用该null参数上的image.getWidth时,传递给create缩略图的参数为null和NPE
如果您检查控制台,我猜您使用emulator进行调试,您可能会注意到来自e1.printStackTrace的堆栈跟踪;在下载图像的方法。检查它告诉您的信息,并尝试找出下载中出现的问题我在浏览器的地址栏中键入了以下内容:,我得到了一个显示照片的正确网页。我右键点击了这个页面,我得到了它的源代码,它给出了:我想你刚刚回答了你自己的问题。downloadphoto.php提供的不是图像,而是包含标记和图像真实路径的html。我用模拟器对其进行了调试,是image.createImage造成了问题。@AndyFrédéric你知道image.createImage中有什么问题吗?例外还是零拉维姆?还是长度参数?还有什么?我找到了解决办法!!!我的错误在于url值的设置:我在谷歌上搜索了j2me下载照片,发现了一个教程,其中展示了设置url的方法。就是这样,;所以我在代码中设置url的方式是错误的。现在,在设置正确的url后,它可以正常工作!!!谢谢
private Image downloadImage(String url) throws IOException
{
Image img = null;
byte[] rawImg = null;
try
{
String imageData = getDataFromUrl(url);
rawImg = imageData.getBytes();
putPhotoToPhone(rawImg);
img = Image.createImage(rawImg, 0, rawImg.length );
}
catch(Exception e1) {
e1.printStackTrace();
}
return img;
}
public String getDataFromUrl(String url) throws IOException {
StringBuffer b = new StringBuffer();
InputStream is = null;
HttpConnection c = null;
long len = 0 ;
int ch = 0;
c = (HttpConnection)Connector.open(url);
is = c.openInputStream();
len = c.getLength();
if( len != -1)
{
for(int i =0 ; i < len ; i++ )
{
if((ch = is.read()) != -1)
{
b.append((char) ch);
}
}
}
else
{
while ((ch = is.read()) != -1)
{
len = is.available() ;
b.append((char)ch);
}
}
is.close();
c.close();
return b.toString();
}
private void putPhotoToPhone(byte[] rawImg)
{
FileConnection fcDir, fcFile;
int photoId, photoNextCounter;
String fileName;
OutputStream os;
if (rawImg != null)
{
try {
fcDir = (FileConnection) Connector.open("file:///"+pRoot+photoDirectory+"/", Connector.READ_WRITE);
if(!fcDir.exists())
fcDir.mkdir();
if (vPhotosName.isEmpty())
photoNextCounter = 1;
else
photoNextCounter = 1;
//photoNextCounter = getNextImageCounter(fcDir, String.valueOf(backForm.vPhotosName.elementAt(backForm.vPhotosName.size()-1)));
fileName = "photo_downloaded_" + photoNextCounter + ".png";
fcFile = (FileConnection) Connector.open("file:///"+pRoot+photoDirectory+"/"+fileName, Connector.READ_WRITE);
if(!fcFile.exists())
fcFile.create();
os = fcFile.openOutputStream();
os.write(rawImg);
os.close();
fcFile.close();
fcDir.close();
try {
photoId = rsImage.addRecord(rawImg, 0, rawImg.length);
vRawPhotoIDs.addElement(new Integer(photoId));
}
catch (RecordStoreException ex) {}
vPhotosName.addElement(fileName);
}
catch (Exception e) {}
}
}
public Image createThumbnail(Image image) {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
int thumbWidth = 50;
int thumbHeight = -1;
if (thumbHeight == -1) {
thumbHeight = thumbWidth * sourceHeight / sourceWidth;
}
Image thumb = image.scaled(thumbWidth, thumbHeight);
return thumb;
}