毕加索图像不显示

Picasso image not displaying

一张图片显示在我的应用程序上,另一张没有显示,但是都可以从浏览器访问。

我的免费主机中的这个没有显示在我的应用程序中。请注意,我可以看到来自免费主机服务器的图像:

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

这个来自一个随机网站并显示在应用程序上。

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

我的问题,会不会是我上传图片的方式有问题?或者可以是免费主机 (000webhost)?请给我你的意见

我正在将图片上传到服务器然后显示在 activity 我可以看到 url 但它没有显示在应用程序中。但是我还添加了另一张图片 url 及其在应用程序中的显示。

这是获取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;
        }

这是上传图片

}
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 文件

$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……';

更新:

我发现 Picasso 不显示您的图像的主要原因是您 JSON 中的图像 Url 不是以 http:// 开头。

这是我的工作示例代码(仅用于获取图像 Url 并由 Picasso 显示,不包括图像上传)

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);            
    }
}

结果截图如下(大家可以注意textView的值,没有http://前缀)

P/S: GET 请求也有效(您可以通过任何网络浏览器访问 http://justedhak.comlu.com/get-data.php 来检查,例如 Chrome)

总而言之,您将有两个选择:

  • 选项 1: 更新服务器数据库中的图像 Urls,以便所有图像都以 http://
  • 开头
  • 选项 2: 检查您的 android 客户端代码中的图像 Urls,如果不是以 http:// 开头,则必须添加此前缀如我上面的示例代码

更新结束


我认为您可以尝试使用 HttpGet 而不是 HttpPost 来获取图像 Urls。我刚刚尝试并得到以下结果;

{"result":[{"id":"1","name":null,"path":"http://api.androidhive.info/json/movies/1.jpg"},{"id":"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":null,"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":空,"path":"www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"19","name":空,"path":"www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"18","name":null,"path":"www.justedhak.comlu.com/images/uploaded_images.jpg"}]}

此外,尝试使用 HttpUrlConnection、OkHttp... 而不是 Apache HttpClient,因为它现在已被弃用。

希望对您有所帮助!