dsai1991 2016-08-11 10:48
浏览 67
已采纳

图像未在android中的imageview中显示

I want to download image from mysql database and display in android image view but it is not being displayed.Following are my java file and php file.There is no error but in my logcat i get factory return null..I can't find where am i going wrong.Thanks in advance...

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

and this is my php code

    <?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条回答 默认 最新

  • dream_life5200 2016-08-11 11:01
    关注

    The base64 decode is missing from this line:

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

    Or you should remove base64_encode the from the PHP:

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

    Other problem: also you should consider using prepared statements in your SQL query! Your code is vulnerable to SQLi.

    $sql = "select * from image where id = '$id'";

    Or you should use some input validation on $id like is_numeric.

    Edit: try the following code:

    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: based on user6704378s comment the image was too large. On smaller images it worked well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配