私はhttps://www.themoviedb.org/からアクティビティに表示されるjpgファイルを含む情報をダウンロードしているAndroidプロジェクトに取り組んでいます。すべてがjpgパスが空であることを除いて、すべてのものが期待どおりに動作しているため、ポスターの代わりに空白の四角が表示されます。構築されたURLの 例JPGが提供されています: http://image.tmdb.org/t/p/w185/3cvZAUVP0gjP0p54TOVXQ50fEyZ.jpg 構築されたURLの例JPGは使用できません。 http://image.tmdb.org/t/p/w185nullAndroid - url image jpgが存在しない場合にローカルデフォルト画像を表示するにはどうすればよいですか?
だから、私は単純に位置ローカルPNGファイルでヌル1を置き換えることができると思いました私の描画可能なフォルダには、しかし、私は
if (poster.equals("http://image.tmdb.org/t/p/w185null")) {
poster = "@drawable/noposter.png";
}
しかしアンドロイドモニターで、私はそれを文字列として認識されて見ることができるという内容を含め、多くの異なるアプローチを試してみました。 私はアンドロイドの開発者のコードでアクセスするリソースセクションを読んでいますが、私の特定の必要性はカバーしていません。
必要に応じて、デフォルトの画像を表示できないようです。
私はあなたの助けに大変感謝します。
がダウン以下の作業が、何の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;
}
}
を使用しています。画像をロードする前に画像を配置するために使用できるプレースホルダ機能を提供します。 –