0
背景:如何識別文件是否存在於我的網絡服務器文件夾或沒有在android系統
我有一個顯示在Android從Web服務器的一些圖像的典型要求。網絡服務器中的所有圖像都是按順序編號的,我現在可以在android中顯示圖片。
問題: 是否有可能確定圖像是否存在於網絡服務器?如果沒有圖像,然後我得到沒有圖像和它沒有顯示我的錯誤
代碼:
公共類ImageViewFromURLActivity延伸活動{
public static final String URL =
"http://mywebsite.com/private/image1.jpg"; //this is just an example
ImageView imageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView);
// Create an object for subclass of AsyncTask
GetXMLTask task = new GetXMLTask();
// Execute the task
task.execute(new String[] { URL });
}
private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap map = null;
for (String url : urls) {
map = downloadImage(url);
}
return map;
}
// Sets the Bitmap returned by doInBackground
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.
decodeStream(stream, null, bmOptions);
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
}
}
,您在將圖像從服務器轉換成BitmapDrawable或繪製對象?如果是的話,你必須得到那個Drawable null,這意味着圖像不在服務器上。 – Amrut
你可以做一個http頭請求 –
嗨@Amrut我已經更新了code.could你pelase看看它? – Yogamurthy