毕加索图片无法显示

3

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

在我的免费主机上的这张图片无法在我的应用程序中显示。请注意,我可以从免费主机服务器上看到该图片:

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

这是从一个随机网站上获取的内容,它正在应用程序中显示。
http://api.androidhive.info/json/movies/1.jpg

我的问题是,我上传图片的方式有问题吗?还是免费主机(000webhost)有问题?请给我您的意见。
我将图片上传到服务器,然后在活动中显示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……';

嘿!我打开了这个链接,但是只看到了一个白屏。这个链接 -> http://www.justedhak.comlu.com/uploaded_image.jpg 显示了一张安卓图片。 - Asis
您上传的图像没有任何数据,这意味着您的上传链路存在问题(您在服务器端使用哪个上传脚本?您是否已经设置了上传脚本的权限?)请发布您的 upload_image.php 文件。 - bonnyz
@Asis,链接有误,我已经编辑了我的问题,正确的链接是http://www.justedhak.comlu.com/images/uploaded_images.jpg。 - Moudiz
在Android中处理图像时,使用Picasso效果非常好。 - Asis
如果我是你,我会尝试使用OkHttpClient。它有很多选项,对我来说总是有效的。特别是与Picasso结合使用。 - Andrei T
显示剩余8条评论
1个回答

2

更新:

我发现 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://前缀)。

BNK's screenshot

提示:GET请求也可以使用(您可以通过访问http://justedhak.comlu.com/get-data.php,例如Chrome等任何Web浏览器来检查)

总之,您将有两个选项:

  • 选项1:更新您服务器数据库中的图像URL,使它们都以http://开头
  • 选项2:检查您的Android客户端代码中的图像URL,如果没有以http://开头,则必须像上面的示例代码一样添加此前缀

更新结束


我认为你可以尝试使用HttpGet而不是HttpPost来获取图像URL。我刚刚尝试了一下,得到了以下结果;

{"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":null,"path":"www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"19","name":null,"path":"www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"18","name":null,"path":"www.justedhak.comlu.com/images/uploaded_images.jpg"}]}

此外,由于Apache HttpClient现在已被弃用,请尝试使用HttpUrlConnection、OkHttp等替代方法。

希望这可以帮到你!

我将其更改为HttpGet,但它没有起作用,我清除了缓存。我添加了一个系统输出,图像的URL被传递给Picasso。但是图片没有加载。如果您有任何问题,请告诉我。 - Moudiz
尝试使用简单的ImageView而不是Picasso来检查图像是否可以显示。 - BNK
你可以将它发布到GitHub或Google Drive或OneDrive上,这样我就可以下载并在明天早上检查了。抱歉,我现在很忙,需要帮助我的妻子 :) - BNK
1
非常感谢您的支持,如果这里没有人能够帮我,明天我会将其发布到 GitHub 上。谢谢! - Moudiz
1
哇,它起作用了,你真聪明。谢谢你。我只想知道你是怎么发现的。 - Moudiz
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接