Picasso image is not displayed

one image is displayed in my application and the other is not, however both can be accessed from the browser.

this my free host is not showing up in my application. Please, not that I can see the image from the free host server:

   http://www.justedhak.comlu.com/images/uploaded_images.jpg

      

this is one of a random site and showing it in the app.

http://api.androidhive.info/json/movies/1.jpg

      

My question is, can the problem be with how I load the image? or maybe a free host (000webhost)? please give me your opinion

I upload image to server and then render in action, I can see url but not show it in app. however I also add another image url and show it in the app.

this is the code to get the image url

protected void showList(){
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray(TAG_RESULTS);

        for(int i=0;i<peoples.length();i++){
            JSONObject c = peoples.getJSONObject(i);
            String id = c.getString(TAG_ID);
            String url = c.getString(TAG_PATH); 
            Listitem.add(new Listitem(id,url));
        }

        GridViewAdapter adapter = new GridViewAdapter(this, R.layout.grid_item_layout, Listitem);
     //   gridView.setAdapter(gridAdapter); 

       list.setAdapter(adapter);

    } catch (JSONException e) {
        e.printStackTrace();
    }

}
public class GetDataJSON extends AsyncTask<String, Void, String>{
     @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://justedhak.comlu.com/get-data.php");

            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (Exception e) {
                // Oops
            }
            finally {
                try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
            }
            return result;
        }

      

this is image loading

}
public void onclick(View view)
{
    Toast.makeText(AddImage.this, "Uploading Image", Toast.LENGTH_LONG).show();                         
    upload();

      Intent i = new Intent(this,
                MainActivity.class);
      startActivity(i);
}
public void upload()
{
      Calendar thisCal = Calendar.getInstance();
      thisCal.getTimeInMillis();

      //  android.util.Log.i("Time Class ", " Time value in millisecinds "+ thisCal);

   // Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);  
 //   ByteArrayOutputStream stream = new ByteArrayOutputStream();
   // bmp.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.

        Intent intent = getIntent();
        String selectedImage= intent.getStringExtra("imagePath");
        Uri fileUri = Uri.parse(selectedImage);

   // Uri selectedImage = intent.getData();
    System.out.println(fileUri);
    InputStream imageStream = null;
    try {
        imageStream = getContentResolver().openInputStream(fileUri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    Bitmap bmp = BitmapFactory.decodeStream(imageStream);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 30, stream);


    byte[] byteArray = stream.toByteArray();
    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    imageview.setImageBitmap(bitmap);
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    System.out.println(width);
    System.out.println(height);


    getResizedBitmap( bitmap, 200);
    try {
        stream.close();
        stream = null;
    } catch (IOException e) {

        e.printStackTrace();
    }

    String image_str = Base64.encodeBytes(byteArray);
    final ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("image",image_str));
    nameValuePairs.add(new BasicNameValuePair("caption",caption));
    nameValuePairs.add(new BasicNameValuePair("name","je"));
    nameValuePairs.add(new BasicNameValuePair("categorie",categorie));
     Thread t = new Thread(new Runnable() {

    @Override
    public void run() {
          try{

                 HttpClient httpclient = new DefaultHttpClient();
                 HttpPost httppost = new HttpPost("http://justedhak.comlu.com/images/upload_image.php");
                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                 HttpResponse response = httpclient.execute(httppost);
                 final String the_string_response = convertResponseToString(response);
                 runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Toast.makeText(AddImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();                         
                        }
                    });

             }catch(final Exception e){
                  runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(AddImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();                             
                    }
                });
                   System.out.println("Error in http connection "+e.toString());
             }  
    }
});
 t.start();
}

      

php file

$con = mysqli_connect($host,$uname,$pwd,$db);

$description= $_GET['description'];
$categorie= $_GET['categorie'];
$imageurl="www.justedhak.comlu.com/images/uploaded_images.jpg";
$image=$_POST['image'];



     $binary=base64_decode($image);
    header('Content-Type: bitmap; charset=utf-8');
    $file = fopen('uploaded_images.jpg', 'wb');
    fwrite($file, $binary);
    fclose($file);

$sql = "insert into image (description,categorie,path) values ('$categorie','$description','$imageurl')";
 if(mysqli_query($con,$sql)){
  echo 'success';
}
else{
echo 'failure';
  }
mysqli_close($con);

    echo 'Image upload complete!!, Please check your php file directory……';

      

+3


source to share


1 answer


UPDATE:

I found the main reason Picasso is not displaying your image is that the Url image in your JSON does not start with http://

.

Here is my working example code (only for getting Url image and Picasso display, not including uploading images)

public class MainActivity extends AppCompatActivity {

    private final Context mContext = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView textView = (TextView) findViewById(R.id.textView);
        final ImageView imageView = (ImageView) findViewById(R.id.imageView);
        String url = "http://justedhak.comlu.com/get-data.php";
        RequestQueue queue = Volley.newRequestQueue(mContext);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (response != null && !response.isNull("result")) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("result");
                        if (jsonArray != null && jsonArray.length() > 0) {
                            // get only one element for testing display image
                            JSONObject jsonObject = jsonArray.getJSONObject(1);
                            if (jsonObject != null && !jsonObject.isNull("path")) {
                                String imagePath = jsonObject.getString("path");
                                if (imagePath != null && !imagePath.isEmpty()) {
                                    textView.setText(imagePath);
                                    Picasso.with(mContext).load("http://" + imagePath).into(imageView);
                                }
                            }
                        }
                    } catch (JSONException e) {
                        textView.setText(e.toString());
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                textView.setText(error.toString());
            }
        });

        queue.add(jsonObjectRequest);            
    }
}

      

And here is a screenshot of the result (you can pay attention to the value textView

without the prefix http://

)

Screenshot BNK

P / S: GET also works (you can check by accessing http://justedhak.comlu.com/get-data.php

just any web browser like Chrome)

To summarize, you have 2 options:



  • Option 1: refresh the image. Called on the server database so that they all start withhttp://

  • Option 2. Check the image. Urls in your android client code, if not started with http://

    , should add this prefix like in the above code example

END OF UPDATE


I think you can try using HttpGet instead of HttpPost to get the url of the image. I just tried it and got the following results:

{"result": [{"ID": "1", "name": NULL, "path": "http://api.androidhive.info/json/movies/1.jpg"}, {"identifier" : "2", "name": null, "path": "www.justedhak.comlu.com/images/uploaded_images.jpg"}, {"id": "28", "name": null, "path" : "www.justedhak.comlu.com/images/uploaded_images.jpg"}, {"id": "27", "name": null, "path": "www.justedhak.comlu.com/images/uploaded_images. jpg "}, {" id ":" 26 "," name ": null," path ":" www.justedhak.comlu.com/images/uploaded_images.jpg "}, {" id ":" 25 "" name ": zero," path ":" www.justedhak.comlu.com / images / uploaded_images.jpg "}, {" id ":" 24 "," name ": null," path ":" www.justedhak.comlu.com/images/uploaded_images.jpg "}, {" ID " : "23", "name": null, "path": "www.justedhak.comlu.com/images/uploaded_images.jpg"}, {"ID": "22", "name": null, "path" : "www.justedhak.comlu.com/images/uploaded_images.jpg"}, {"id": "21", "name": null, "path": "www.justedhak.comlu.com/images/uploaded_images. jpg "}, {" id ":" 20 "," name ": null," path ":" www.justedhak.comlu.com/images/uploaded_images.jpg "}, {" id ":" 19 "," name ": null," path ":" www.justedhak.comlu.com/images/uploade d_images.jpg "}, {" id ":" 18 "," name ": NULL," path ":" www.justedhak.comlu.com/images/uploaded_images.jpg "} ]}

Also, try using HttpUrlConnection, OkHttp ... instead of Apache HttpClient due to deprecation.

Hope this helps!

+2


source







All Articles