dsai1991 2016-08-11 02: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 03: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部