簡體   English   中英

如何在不轉換為Android的Base64的情況下將圖像上傳到服務器?

[英]how to upload image to server without converting to Base64 in Android?

我是android studio的新手。 我想將圖像上傳到服務器而不將其轉換為base64字符串。 我不知道該怎么做。 這是我的代碼:

//This is my code
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

HttpClient Client = new DefaultHttpClient();
HttpPost Post = new HttpPost("url/uploadphoto.php");

            Log.d("HTTPPSt", "" + Post);
            // Building post parameters
            // key and value pair
            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
            nameValuePair.add(new BasicNameValuePair("image", bitmap));

            // Url Encoding the POST parameters
            try {
                Post.setEntity(new UrlEncodedFormEntity(nameValuePair));

            } catch (UnsupportedEncodingException e) {
                // writing error to Log
                e.printStackTrace();
            }
            // Making HTTP Request
            try {
                HttpResponse response = Client.execute(Post);
                StatusLine statusLine=response.getStatusLine();
                // writing response to log
                Log.d("Http Response:", response.toString());
                Log.d("statusline:", ""+statusLine);

            } catch (ClientProtocolException e) {
                // writing exception to log
                e.printStackTrace();
            } catch (IOException e) {
                // writing exception to log
                e.printStackTrace();
            }

我有這個問題:

nameValuePair.add(new BasicNameValuePair("image", bitmap));

我無法發送位圖,但出現錯誤。

與其自己編寫,不如考慮使用庫。

有很好的網絡庫可以做到這一點,包括Retrofit和Volley。

如果您看的話,我確定還有圖像處理庫,其中包括上載和下載功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM