将图像上传到服务器,将URL上传到数据库并返回到imageview

时间:2014-09-05 11:19:00

标签: php android mysql json

我是android的新手!我需要你的帮助。我想创建一个Android应用程序,用户将提供一个图像,该图像将从android imageview,usnig asynctask和json发送到服务器,图像的url将使用php存储到服务器..然后我想要显示它回到android listview。我做了很多研究,但找不到..请真的需要你的帮助。 。谢谢..

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码将图像上传到服务器:

String url = //your URL for the server here;

    MultipartEntity reqEntity = new MultipartEntity(
            HttpMultipartMode.BROWSER_COMPATIBLE);

    File f = new File(Environment.getExternalStorageDirectory(),
            "myImage.jpg");
    FileBody body = null;
    if (f.exists()) {
        body = new FileBody(f);
        reqEntity.addPart(IMAGE, body);
    }
    reqEntity.addPart(IMAGEURL, new StringBody(f.getAbsolutePath());


    String urlString = getResponseStringFromURL(url, 30000, reqEntity);

getresponseStringFromUrl的功能代码是:

/**
     * Requests on Given URL for Response with a Given Timeout
     * 
     * @param reqEntity
     */
    public static String getResponseStringFromURL(String url, int timeOut,
            MultipartEntity reqEntity) {
        StringBuilder result = new StringBuilder();
        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = timeOut;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeoutConnection);
        int timeoutSocket = timeOut;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
        HttpPost request = new HttpPost(url);
        if (reqEntity != null) {
            request.setEntity(reqEntity);
        }
        HttpResponse response = null;

        try {
            response = httpClient.execute(request);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        HttpEntity entity = response.getEntity();
        InputStream input = null;
        try {
            input = new BufferedInputStream(response.getEntity().getContent());
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        byte data[] = new byte[40000];

        long totalContactsCount = -1;
        int readContactsCount = 0;
        int currentByteReadCount = 0;

        /** read response from inpus stream */
        try {
            while ((currentByteReadCount = input.read(data)) != -1) {
                String readData = new String(data, 0, currentByteReadCount);
                result.append(readData);

                // then +1 progress on every ...},{... (JSON object separator)
                if (readData.indexOf("}~{") >= 0) {
                    readContactsCount++;
                }

                /*
                 * // publishing the progress.... if (totalContactsCount > 0) {
                 * publishProgress((int)(readContactsCount * 100 /
                 * totalContactsCount)); }
                 */
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            input.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /** transform response into JSONArray */
        return result.toString();

    }