从android中的名称加载图像

时间:2016-11-09 14:53:19

标签: php android

在我的程序中,我考虑两个页面,在第一页中,我在主机服务器中保存了一个带有人名和图像名称的图像,并将图像保存在手机的文件夹(folder_name)中,有用。 在第二页我要显示文件路径中的图像,我将人名发送到第二页,我想要它搜索服务器并返回图像名称并在第二页显示图像,但是当我触摸btn以显示程序关闭时! 我无法理解为什么我的代码不起作用! 这是我的代码:



package com.example.eagle.start1;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class ShowPicPage extends Activity {

    Button Confirm;
    ImageView img;
    TextView ShowName;
    ProgressDialog pd;
    String Image_path,userpic;
    String Name;

    private static String url = "*********";
    JSONParser jparser = new JSONParser();
    ArrayList<HashMap<String, String>> P;
    JSONArray s = null;
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";

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

        Confirm = (Button)findViewById(R.id.btnConfirm);
        img = (ImageView)findViewById(R.id.imgPic);
        ShowName = (TextView)findViewById(R.id.etshowname);

        Bundle extras = getIntent().getExtras();
        Name = extras.getString("UserName");

        ShowName.setText(Name);

        Confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new  LoadPic().execute();

            }
        });


    }


    class LoadPic extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = new ProgressDialog(ShowPicPage.this);
            pd.setMessage("loading...");
            pd.show();
        }


        @Override
        protected String doInBackground(String... argman) {

            int success;


            try {



                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("InName", Name));
                JSONObject json = jparser.makeHttpRequest(url, "GET", params);



                success = json.getInt(TAG_SUCCESS);
                // int t = json.getInt("t");

                if (success == 1){

                    s=json.getJSONArray("imagee");
                    for (int i=0;i<s.length();i++){
                        JSONObject c=s.getJSONObject(i);
                        String username=c.getString("UserName");
                        userpic=c.getString("UserImage");

                        HashMap<String,String> map=new HashMap<String,String>();
                        map.put("username",username);
                        map.put("userpict",userpic);

                        P.add(map);
                    }
                    return json.getString(TAG_MESSAGE);


                }else {

                    return json.getString(TAG_MESSAGE);

                }



            } catch (JSONException e) {
                e.printStackTrace();

            }

                    return null;

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            pd.dismiss();


                runOnUiThread(new Runnable() {
                @Override
                public void run() {


                   // ShowName.setText(userpic);
                    Image_path = Environment.getExternalStorageDirectory()+ "/Pictures/folder_name/"+userpic;

                    Log.e("zahraPath", "----------------" + Image_path);

                    File imgFile = new  File(Image_path);
                    if(imgFile.exists())
                    {
                        Log.e("path", "----------------" + Image_path);

                        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                        img.setImageBitmap(myBitmap);
                       // ShowName.setText(Name);
                    }



                }
            });
            if (s != null){
                Toast.makeText(ShowPicPage.this, s, Toast.LENGTH_SHORT).show();
            }



        }

    }




}
&#13;
&#13;
&#13;

这是我的php:

&#13;
&#13;
<?php


$con = mysql_connect("******","*****","****");

mysql_select_db("u341760924_one",$con);

if (isset($_GET['InName'])) {
    $username = $_GET['InName'];
	
	
	$response=array();
    $result=mysql_query("SELECT * FROM imagee WHERE UserName = '$username'");

if(mysql_num_rows($result)>0){

	$response["imagee"]=array();
	
	while($row=mysql_fetch_array($result)){
		$temp=array();
		$temp["UserName"]=$row["UserName"];
		$temp["UserImage"]=$row["IMG"];
		
		
		
		array_push($response["imagee"],$temp);
		
		
	}
	
	$response["success"] = 1;
	$response["message"] = "successfully down";
	echo json_encode($response);

	
	
}else{
	
	$response["success"] = 0;
            $response["message"] = "No picture!";
 
            // echo no users JSON
            echo json_encode($response);
	
}
}
else{
	
	$response["success"] = 0;
            $response["message"] = "No Connect!";
 
            // echo no users JSON
            echo json_encode($response);
	
}

	
	



?>
&#13;
&#13;
&#13;

0 个答案:

没有答案