0

The below code extracts image from a db and displays it on a recyclerview. The code for full image is not displaying the full image. I suspect i cant get the exact URL to parse to FullImageViewe.java.

How to parse the clicked image/text url to show the full image here? All of the depedencies are ok.

Recyclerview Cardview

Full Image View

Main activity

package com.ny.fetchallimages;

import android.content.Intent;
import android.os.Bundle;
import org.json.JSONArray;
import java.util.ArrayList;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import java.util.List;

import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    List<DataAdapter> ListOfdataAdapter;

    RecyclerView recyclerView;

    String HTTP_JSON_URL = "http://ny.com/uploaddownload/fetchallimages.php";

    String Image_URL_JSON = "image_data";

    String Image_Name_JSON = "image_tag";

    String Image_Time_JSON = "time";

    JsonArrayRequest RequestOfJSonArray ;

    RequestQueue requestQueue ;

    View view ;

    int RecyclerViewItemPosition ;

    RecyclerView.LayoutManager layoutManagerOfrecyclerView;

    RecyclerView.Adapter recyclerViewadapter;

    ArrayList<String> ImageTitleNameArrayListForClick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ImageTitleNameArrayListForClick = new ArrayList<>();

        ListOfdataAdapter = new ArrayList<>();

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);

        recyclerView.setHasFixedSize(true);

        layoutManagerOfrecyclerView = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(layoutManagerOfrecyclerView);

        JSON_HTTP_CALL();

        // Implementing Click Listener on RecyclerView.
        recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

            GestureDetector gestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {

                @Override public boolean onSingleTapUp(MotionEvent motionEvent) {

                    return true;
                }

            });
            @Override
            public boolean onInterceptTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {
                view = Recyclerview.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
                if(view != null && gestureDetector.onTouchEvent(motionEvent)) {
                    //Getting RecyclerView Clicked Item value.
                       RecyclerViewItemPosition = Recyclerview.getChildAdapterPosition(view);
                       Intent intent = new Intent(MainActivity.this, FullImageViewer.class );
                       intent.putExtra("img", "HTTP_JSON_URL");
                       startActivity(intent);

                }
                return false;
            }
            @Override
            public void onTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {

            }
            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
            }
        });
    }
    public void JSON_HTTP_CALL(){
        RequestOfJSonArray = new JsonArrayRequest(HTTP_JSON_URL,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        ParseJSonResponse(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        requestQueue = Volley.newRequestQueue(MainActivity.this);
        requestQueue.add(RequestOfJSonArray);
    }
    public void ParseJSonResponse(JSONArray array){
        for(int i = 0; i<array.length(); i++) {

            DataAdapter GetDataAdapter2 = new DataAdapter();

            JSONObject json = null;
            try {
                json = array.getJSONObject(i);

                GetDataAdapter2.setImageTitle(json.getString(Image_URL_JSON));
                // Adding image title name in array to display on RecyclerView click event.
                ImageTitleNameArrayListForClick.add(json.getString(Image_URL_JSON));
                GetDataAdapter2.setImageUrl(json.getString(Image_URL_JSON));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            ListOfdataAdapter.add(GetDataAdapter2);
        }
        recyclerViewadapter = new RecyclerViewAdapter(ListOfdataAdapter, this);
        recyclerView.setAdapter(recyclerViewadapter);
    }
}

Fullimageviewer

package com.ny.fetchallimages;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

public class FullImageViewer extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fullimageviewer);
        final ImageView img = findViewById(R.id.img);
        String url = (String) getIntent().getSerializableExtra("img");

        //Glide library to load image from URL
        Glide.with(getApplicationContext())
                .asBitmap()
                .load(url)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        img.setImageBitmap(resource);
                       }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }
}

Fullimageviewer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:background="@color/color1">

    <ImageView
        android:id="@+id/img"
        android:padding="5dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true" />
</RelativeLayout>
James Z
  • 12,209
  • 10
  • 24
  • 44
NyP
  • 499
  • 4
  • 18
  • Try to change `intent.putExtra("img", "HTTP_JSON_URL");` to `intent.putExtra("img", HTTP_JSON_URL);` at MainActivity recyclerview onInterceptTouchEvent then you can add `Log.d("url image", url.toString); `at bottom of `String url = (String) getIntent... ` to now the right url, hope this help – adi purnama Jan 08 '20 at 00:17

2 Answers2

0

I think I know whats going on here. The link you used "http://ny.com/uploaddownload/fetchallimages.php" is in HTTP format and not https and somewhere down the line google stopped supporting HTTP in their apps until you clarify that in the app manifest. That can be done using this:

android:usesCleartextTraffic="true"

in your android manifest somewhat like this:

AndroidManifest.xml:

<manifest>
<application

        android:allowBackup="true"
        android:icon="@drawable/appicon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">
</manifest>

I too had this issue down the road while devloping my app and found this solution here it also has another solution that is more safer but i couldnt get that to work.

Hope I helped. (btw I too have a question and you seem skilled enough so please have a look at it.

Amitoj Singh
  • 59
  • 1
  • 3
0

Thanks at Amitoj. Will look in your issue though i am not conversant with IOS Xcode. and I Got it finally. Thanks to @Mouaad Abdelghafour AITALI for pointing to the right direction. The answer lied in tthe correct recyclerview position URL. RecyclerViewItemPosition = Recyclerview.getChildAdapterPosition(view); Intent intent = new Intent(MainActivity.this, FullImageViewer.class ); intent.putExtra("img", RecyclerViewItemPosition = Recyclerview.getChildAdapterPosition()); startActivity(intent);

NyP
  • 499
  • 4
  • 18