当我点击项目中的图像时,会加载另一个图像。它工作正常,但当我回到上一个活动,并点击相同的图像,它不会被加载。
这是应用处于活动状态时打开的第一个活动。此页面将显示图片网格
public class GentsActivity extends Fragment implements AdapterView.OnItemClickListener {
//Web api url
public static final String DATA_URL = "PHP LINK HERE";
//Tag values to read from json
public static final String TAG_IMAGE_URL = "small_image_url";
//GridView Object
private GridView gridView;
//ArrayList for Storing image urls and titles
private ArrayList<String> images;
private SwipeRefreshLayout swipeContainer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
View view= inflater.inflate(R.layout.activity_gents, container, false);
gridView = (GridView) view.findViewById(R.id.gridView);
getData();
//swipeContainer = (SwipeRefreshLayout) view.findViewById(R.id.swipeContainer);
images = new ArrayList<>();
//Calling the getData method
/*swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// Your code to refresh the list here.
// Make sure you call swipeContainer.setRefreshing(false)
// once the network request has completed successfully.
//Toast.makeText(this,"refresh ",Toast.LENGTH_SHORT).show();
Intent mIntent= new Intent(SareeActivity.this,SareeActivity.class);
startActivity(mIntent);
swipeContainer.setRefreshing(false);
}
});*/
return view;
}
private void getData(){
//Showing a progress dialog while our app fetches the data from url
//final ProgressDialog loading = ProgressDialog.show(this, "Please wait,","Fetching data.",false,false);
//Creating a json array request to get the json from our api
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Dismissing the progressdialog on response
// loading.dismiss();
//Displaying our grid
showGrid(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
//Adding our request to the queue
requestQueue.add(jsonArrayRequest);
}
private void showGrid(JSONArray jsonArray){
//Looping through all the elements of json array
for(int i = 0; i<jsonArray.length(); i++){
//Creating a json object of the current index
JSONObject obj = null;
try {
//getting json object from current index
obj = jsonArray.getJSONObject(i);
// Log.d(TAG_IMAGE_URL,"JSON SHOW GRID"+obj);
//getting image url and title from json object
images.add(obj.getString(TAG_IMAGE_URL));
Log.d(TAG_IMAGE_URL,"JSON SHOW GRID"+images);
} catch (JSONException e) {
e.printStackTrace();
}
}
//Creating GridViewAdapter Object
//Adding adapter to gridview
GridViewAdapter gridViewAdapter = new GridViewAdapter(getContext(),images);
gridView.setAdapter(gridViewAdapter);
gridView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String prompt = (String)adapterView.getItemAtPosition(i);
Intent mIntent= new Intent(getActivity(),LoadPhotoGents.class);
mIntent.putExtra("s",prompt);
startActivity(mIntent);
}
}
当我点击某张特定照片时,该单张照片将会打开。代码如下:
public class LoadPhotoGents extends AppCompatActivity {
private String data, path;
private ImageView ivi;
public static final String DATA_URL = "PHP LINK HERE";
private static int id=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_photo_gents);
data = getIntent().getExtras().getString("s");
path = data.replace(".JPG", "big.JPG");
//Toast.makeText(this, "Path:" + path, Toast.LENGTH_LONG).show();
ivi = (ImageView) findViewById(R.id.fullImage);
Picasso.with(LoadPhotoGents.this).load(path).into(ivi);
getData();
ImageViewTouch img = (ImageViewTouch) findViewById(R.id.fullImage);
img.setBackgroundColor(Color.parseColor("#000000"));
ivi.buildDrawingCache();
Bitmap bmap=ivi.getDrawingCache();
//img.setFitToScreen(true);
img.setImageBitmap(bmap);
}
private void getData(){
String url=DATA_URL+data.trim();
StringRequest stringRequest=new StringRequest(url,new Response.Listener<String>(){
@Override
public void onResponse(String response){
showJSON(response);
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
}
});
RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String name= "";
try{
JSONArray jsonArray=new JSONArray(response);
//JSONArray result= jsonObject.getJSONArray("result");
JSONObject datas=jsonArray.getJSONObject(0);
name=datas.getString("description");
}catch(JSONException e){
Toast.makeText(this,"inside getData: "+name,Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:0)
确保路径变量不为null或为空
答案 1 :(得分:0)
这肯定会帮助您调试问题。在您的代码中,我可以看到您使用Picasso.with().load().into()
问题直接加载图像,此方法是您不知道后台发生了什么。
你可以做两件事。将图像加载到ImageView中时,首先使用Callback
Picasso.with(LoadPhotoGents.this).load(path).into(ivi, new Callback()
{
@Override
public void onSuccess()
{
Timber.d("Image Loaded Successfully");
}
@Override
public void onError()
{
Timber.d("Error Loading Image");
}
});
以上可用于处理图像加载/未加载的场景。
现在在加载图片时遇到实际错误,您需要使用一个Picasso.Builder
,它有一个监听器,可以帮助您了解实际错误。
如下构建Picasso Builder
Picasso.Builder builder = new Picasso.Builder(mContext);
builder.listener(new Picasso.Listener()
{
@Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception)
{
Timber.d(exception.getMessage());
}
});
Picasso pic = builder.build();
要加载图像,请执行以下操作
pic.load(path).into(ivi, new Callback()
{
@Override
public void onSuccess()
{
Timber.d("Image Loaded Successfully");
}
@Override
public void onError()
{
Timber.d("Image Load Error");
}
});