我正在开发一个Android项目,我正在从https://www.themoviedb.org/下载信息,包括要在活动中显示的jpg文件。一切都按预期工作,除了jpg路径是空的,导致一个空白的方块而不是海报。 jpg可用时构造的url示例: http://image.tmdb.org/t/p/w185/3cvZAUVP0gjP0p54TOVXQ50fEyZ.jpg 当jpg不可用时构造的url的示例: http://image.tmdb.org/t/p/w185null
所以我想我可以简单地用我的drawable文件夹中的本地png文件替换null,但是我尝试了很多不同的方法,包括声明
if (poster.equals("http://image.tmdb.org/t/p/w185null")) {
poster = "@drawable/noposter.png";
}
然而在android监视器中我可以看到它被识别为字符串。 我在代码部分阅读了android开发人员的访问资源,但它并未涵盖我的具体需求。
如果需要,我似乎无法显示默认图片。
非常感谢你的帮助。
下面是代码正常工作但如果没有可用的jpg则显示空白方块:
public class GetMoviesInfo extends AsyncTask<String, Void, List<Movies>> {
public InterfaceAsyncResponse delegate;
private final String LOGTAG = GetMoviesInfo.class.getSimpleName();
private final String API_KEY_HEX_NUM = "a valid key is here in my project";
private final String MOVIE_POSTER_BASE_URL = "http://image.tmdb.org/t/p/";
private final String CHOSEN_MOVIE_POSTER_SIZE = "w185";
public GetMoviesInfo(InterfaceAsyncResponse delegate){
this.delegate = delegate;
}
@Override
protected List<Movies> doInBackground(String... parameters) {
if (parameters.length == 0) {
return null;
}
HttpURLConnection urlConnection = null;
BufferedReader inputReader = null;
String moviesStr = null;
try {
final String BASEURL = "http://api.themoviedb.org/3/discover/movie?";
final String SORT_BY_KEY = "sort_by";
final String APIKEY = "api_key";
String sortedBy = parameters[0];
Uri buildUri = Uri.parse(BASEURL).buildUpon()
.appendQueryParameter(SORT_BY_KEY, sortedBy)
.appendQueryParameter(APIKEY, API_KEY_HEX_NUM)
.build();
URL url = new URL(buildUri.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer bufferStr = new StringBuffer();
if (inputStream == null) {
return null;
}
inputReader = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = inputReader.readLine()) != null) {
bufferStr.append(inputLine + "\n");
}
if (bufferStr.length() == 0) {
return null;
}
moviesStr = bufferStr.toString();
} catch (IOException e) {
Log.e(LOGTAG, "Error ", e);
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputReader != null) {
try {
inputReader.close();
} catch (final IOException e) {
Log.e(LOGTAG, "Error closing stream", e);
}
}
}
try {
return extractData(moviesStr);
} catch (JSONException e) {
Log.e(LOGTAG, e.getMessage(), e);
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<Movies> returns) {
if (returns != null) {
delegate.onTaskCompleted(returns);
}
}
private String getReleasedYear(String dateReleased){
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
final Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(dateFormat.parse(dateReleased));
} catch (ParseException e) {
e.printStackTrace();
}
return Integer.toString(calendar.get(Calendar.YEAR));
}
private List<Movies> extractData(String moviesJsonString) throws JSONException {
// Items being extracted
final String MOVIES_ARRAY = "results";
final String ORIGINAL_TITLE = "original_title";
final String POSTER_PATH = "poster_path";
final String OVERVIEW = "overview";
final String VOTE_AVERAGE = "vote_average";
final String RELEASED_DATE = "release_date";
final String POPULARITY = "popularity";
JSONObject moviesJson = new JSONObject(moviesJsonString);
JSONArray moviesArray = moviesJson.getJSONArray(MOVIES_ARRAY);
int moviesLength = moviesArray.length();
List<Movies> movies = new ArrayList<>();
for(int i = 0; i < moviesLength; ++i) {
// For each movie, the JSON object create a new movie object
JSONObject movie = moviesArray.getJSONObject(i);
String title = movie.getString(ORIGINAL_TITLE);
String poster = MOVIE_POSTER_BASE_URL + CHOSEN_MOVIE_POSTER_SIZE + movie.getString(POSTER_PATH);
Log.i("TAG","From GetMoviesInfo_extractData: "+ poster);
String overview = movie.getString(OVERVIEW);
String voteAverage = movie.getString(VOTE_AVERAGE);
String releasedDate = getReleasedYear(movie.getString(RELEASED_DATE));
String popularity = movie.getString(POPULARITY);
movies.add(new Movies(title, poster, overview, voteAverage, releasedDate, popularity));
}
return movies;
}
}
答案 0 :(得分:1)
你可以使用毕加索库来完成你的目标,使用我很简单的共享代码。 有关更多信息,请使用此http://square.github.io/picasso/
> Picasso.with(context)
> .load(url)
> .placeholder(R.drawable.user_placeholder)
> .error(R.drawable.user_placeholder_error)
> .into(imageView);