图像未在android中的imageview中显示

时间:2016-08-11 10:48:47

标签: php android mysql

我想从mysql数据库下载图像并在android图像视图中显示但它没有被显示。以下是我的java文件和php文件。没有错误但是在我的logcat我得到工厂返回null ..我可以找不到我错的地方。谢谢你提前......

package com.example.admin.myapplication;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by admin on 8/10/2016.
 */
public class ViewImages extends Activity implements View.OnClickListener {

private EditText editTextId;
private Button buttonGetImage;
private ImageView imageView;

private requesthandler requestHandlers;


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

    editTextId = (EditText) findViewById(R.id.editTextId);
    buttonGetImage = (Button) findViewById(R.id.buttonGetImage);
    imageView = (ImageView) findViewById(R.id.imageViewShow);

    requestHandlers = new requesthandler();

    buttonGetImage.setOnClickListener(this);
}


private void getImage() {
    String id = editTextId.getText().toString().trim();
    class GetImage extends AsyncTask<String,Void,Bitmap>{
        ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(ViewImages.this, "Uploading...", null,true,true);
        }

        @Override
        protected void onPostExecute(Bitmap b) {
            super.onPostExecute(b);
            loading.dismiss();
            imageView.setImageBitmap(b);
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            String id = params[0];
            String add = "http://10.0.2.2/project/getimage.php?id="+id;
            URL url = null;
            Bitmap image = null;

            try {
                url = new URL(add);
               // byte[] data = Base64.decode(image);
               // image = BitmapFactory.

                image = BitmapFactory.decodeStream(url.openConnection().getInputStream());





            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return image;
        }
    }

    GetImage gi = new GetImage();
    gi.execute(id);
}

@Override
public void onClick(View v) {
    getImage();
}
}

这是我的PHP代码

    <?php
define('HOST','localhost');
  define('USER','root');
  define('PASS','');
  define('DB','vineet');
 $con = mysqli_connect(HOST,USER,PASS,DB);



 if($_SERVER['REQUEST_METHOD']=='GET'){
 $id = $_GET['id'];
 $sql = "select * from image where id = '$id'";


 //require_once('dbConnect.php');

 $r = mysqli_query($con,$sql);

 $result = mysqli_fetch_array($r);

 header('Content-Type: image/jpeg');
echo (base64_encode( $result['image']));





 mysqli_close($con);

 }else{
 echo "Error";


    }

1 个答案:

答案 0 :(得分:0)

此行中缺少base64 decode

  

image = BitmapFactory.decodeStream(url.openConnection().getInputStream());

或者您应该从PHP中删除base64_encode

  

echo (base64_encode( $result['image']));

其他问题:您还应该考虑在SQL查询中使用预处理语句!您的代码容易受到SQLi的攻击。<​​/ p>

  

$ sql =“select * from image where id ='$ id'”;

或者你应该在{id is_numeric.

这个$ id上使用一些输入验证

编辑:尝试以下代码:

BufferedReader in = url.openConnection().getInputStream();
String inputLine;
StringBuilder encodedContent = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
    encodedContent.append(inputLine);
}
in.close();
//dump/log encodedContent here (should be a BASE64 string)!
byte[] decodedBytes = Base64.getDecoder().decode(encodedContent.toString());
image = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);

Edit2:根据user6704378的评论,图片太大了。在较小的图像上它运作良好。