ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 안드로이드 httpurlconnection,으로 이미지 전송하기
    안드로이드 2018. 8. 2. 21:53


    HttpURLConnection 을 이용할 때는 주의해야  할 점이 몇 개 있습니다.


    - mimetype을 수동으로 지정해 주어야 합니다.

    - 일부 초고화질 사진은 업로드 시 서버가 처리하지 못하는 문제가 있습니다. 거기에 exif 정보가 사진을 촬영할때 들어가기에... 그대로 올려 버리면 서버가 적절하게 이미지를 회전시키지 못하는 경우가 있습니다. 서버가 적절한 라이브러리를 찾지 못하는 탓이긴 하지만..

    피차 좋게좋게 가려면 리사이징을 해줄 필요는 있습니다. 


    이미지랑 같이 보낼 파라미터 매핑, 이미지 파일 경로, 이미지의 파일 이름, 이미지의 파일 마임맵까지 첨부한 것.



    public String multipartRequest(String urlTo, Map<String, String> params, String filepath, String filefield, String fileMimeType)  {


            HttpURLConnection conn = null;

            DataOutputStream outputStream = null;

            InputStream inputStream = null;


            String twoHyphens = "--";

            String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";

            String lineEnd = "\r\n";


            String result = "";


            int bytesRead, bytesAvailable, bufferSize;

            byte[] buffer;

            int maxBufferSize = 1 * 1024 * 1024;


            String[] q = filepath.split("/");

            int idx = q.length - 1;


            try {

                File file = new File(filepath);


                FileInputStream fileInputStream = new FileInputStream(file);


                URL url = new URL(urlTo);

                conn = (HttpURLConnection) url.openConnection();


                conn.setDoInput(true);

                conn.setDoOutput(true);

                conn.setUseCaches(false);


                conn.setRequestMethod("POST");

                conn.setRequestProperty("Connection", "Keep-Alive");

                conn.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");

                conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

                conn.setRequestProperty("Cookie", CookieManager.getInstance().getCookie(urlTo)); //쿠키를 실어 보낸다


                outputStream = new DataOutputStream(conn.getOutputStream());

                outputStream.writeBytes(twoHyphens + boundary + lineEnd);

                outputStream.writeBytes("Content-Disposition: form-data; name=\"" + filefield + "\"; filename=\"" + q[idx] + "\"" + lineEnd);

                outputStream.writeBytes("Content-Type: " + fileMimeType + lineEnd);

                outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);


                outputStream.writeBytes(lineEnd);


                bytesAvailable = fileInputStream.available();

                bufferSize = Math.min(bytesAvailable, maxBufferSize);

                buffer = new byte[bufferSize];


                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                while (bytesRead > 0) {

                    outputStream.write(buffer, 0, bufferSize);

                    bytesAvailable = fileInputStream.available();

                    bufferSize = Math.min(bytesAvailable, maxBufferSize);

                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                }


                outputStream.writeBytes(lineEnd);

                // Upload POST Data



                Iterator<String> keys = params.keySet().iterator();

                while (keys.hasNext()) {

                    String key = keys.next();

                    String value = params.get(key);


                    outputStream.writeBytes(twoHyphens + boundary + lineEnd);

                    outputStream.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd);

                    outputStream.writeBytes("Content-Type: text/plain" + lineEnd);

                    outputStream.writeBytes(lineEnd);

                    outputStream.writeBytes(value);

                    outputStream.writeBytes(lineEnd);

                }

                outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);


                inputStream = conn.getInputStream();


                result = this.convertStreamToString(inputStream);


                fileInputStream.close();

                inputStream.close();

                outputStream.flush();

                outputStream.close();


                return result;

            } catch (Exception e) {

                e.printStackTrace();

            }

            return result;

        }


    사용 예제.


    public static String saveBitmapToJpeg(Context context, Bitmap bitmap){

    File storage = context.getCacheDir(); // 이 부분이 임시파일 저장 경로

    String fileName = getRandomString(10) + ".jpg"; // 파일이름은 마음대로!

    File tempFile = new File(storage,fileName);

    try{
    tempFile.createNewFile(); // 파일을 생성해주고

    FileOutputStream out = new FileOutputStream(tempFile);

    bitmap.compress(Bitmap.CompressFormat.JPEG, 90 , out); // 넘거 받은 bitmap을 jpeg(손실압축)으로 저장해줌

    out.close(); // 마무리로 닫아줍니다.

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    return tempFile.getAbsolutePath(); // 임시파일 저장경로를 리턴해주면 끝!
    }


    ListIterator<PostImage> keys = rowListItem.listIterator(rowListItem.size());
    while (keys.hasPrevious()) {
    PostImage item = keys.previous();
    Uri uri= item.getPhoto();
    // String realpaths= getPathFromUri(uri);
    // String mimetype =getActivity().getContentResolver().getType(uri);
    try {
    Bitmap bitmap = Glide.with(this)
    .load(uri)
    .asBitmap()
    .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
    .get(); //글라이드로 리사이징해서 이미지 타입을 jpg로 고정합니다.

    String realpaths = URLRequest.saveBitmapToJpeg(this.getContext(),bitmap);
    String mimetype = "image/jpeg";
    urlRequest.multipartRequest("http://file_to_upload", params, realpaths, "fileName", mimetype);
    } catch (InterruptedException e) {
    e.printStackTrace();
    } catch (ExecutionException e) {
    e.printStackTrace();
    }

    }



    레트로핏으로 하면 더 간단합니다.

     public Call<ArticleModel> postArticle(long point_x, long point_y, String uploadText, MultipartBody.Part image) {

            PointModel pointModel = new PointModel(point_x,point_y);

            RequestBody text = RequestBody.create(MediaType.parse("text/plain"), uploadText);



            return articlePostService.postArticle(point_x,point_y,text,pointModel.getPointN(),pointModel.getPointE(), pointModel.getPointW(), pointModel.getPointS(), image);}




      private File createImageFile() throws IOException {

            // Create an image file name

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

            String imageFileName = "JPEG_" + timeStamp + "_";

            File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

            File image = File.createTempFile(

                    imageFileName,  /* prefix */

                    ".jpg",         /* suffix */

                    storageDir      /* directory */

            );


            // Save a file: path for use with ACTION_VIEW intents

            mCurrentPhotoPath = image.getAbsolutePath();

            return image;

        }


    import android.annotation.SuppressLint;

    import android.annotation.TargetApi;

    import android.content.ContentUris;

    import android.content.Context;

    import android.database.Cursor;

    import android.net.Uri;

    import android.os.Build;

    import android.os.Environment;

    import android.provider.DocumentsContract;

    import android.provider.MediaStore;


    /**

     * Created by sesan on 2018-01-21.

     */


    @SuppressLint("NewApi")

    @TargetApi(Build.VERSION_CODES.KITKAT)

    public class ImageFilePath {


        /**

         * Method for return file path of Gallery image

         *

         * @param context

         * @param uri

         * @return path of the selected image file from gallery

         */

        static String nopath = "Select Video Only";


        @TargetApi(Build.VERSION_CODES.KITKAT)

        @SuppressLint("NewApi")

        public static String getPath(final Context context, final Uri uri) {


            // check here to KITKAT or new version

            final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;


            // DocumentProvider

            if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {


                // ExternalStorageProvider

                if (isExternalStorageDocument(uri)) {

                    final String docId = DocumentsContract.getDocumentId(uri);

                    final String[] split = docId.split(":");

                    final String type = split[0];


                    if ("primary".equalsIgnoreCase(type)) {

                        return Environment.getExternalStorageDirectory() + "/"

                                + split[1];

                    }

                }

                // DownloadsProvider

                else if (isDownloadsDocument(uri)) {


                    final String id = DocumentsContract.getDocumentId(uri);

                    final Uri contentUri = ContentUris.withAppendedId(

                            Uri.parse("content://downloads/public_downloads"),

                            Long.valueOf(id));


                    return getDataColumn(context, contentUri, null, null);

                }

                // MediaProvider

                else if (isMediaDocument(uri)) {

                    final String docId = DocumentsContract.getDocumentId(uri);

                    final String[] split = docId.split(":");

                    final String type = split[0];


                    Uri contentUri = null;

                    if ("image".equals(type)) {

                        contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

                    } else if ("video".equals(type)) {

                        contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

                    } else if ("audio".equals(type)) {

                        contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

                    }


                    final String selection = "_id=?";

                    final String[] selectionArgs = new String[] { split[1] };


                    return getDataColumn(context, contentUri, selection,

                            selectionArgs);

                }

            }

            // MediaStore (and general)

            else if ("content".equalsIgnoreCase(uri.getScheme())) {


                // Return the remote address

                if (isGooglePhotosUri(uri))

                    return uri.getLastPathSegment();


                return getDataColumn(context, uri, null, null);

            }

            // File

            else if ("file".equalsIgnoreCase(uri.getScheme())) {

                return uri.getPath();

            }


            return nopath;

        }


        /**

         * Get the value of the data column for this Uri. This is <span id="IL_AD2"

         * class="IL_AD">useful</span> for MediaStore Uris, and other file-based

         * ContentProviders.

         *

         * @param context

         *            The context.

         * @param uri

         *            The Uri to query.

         * @param selection

         *            (Optional) Filter used in the query.

         * @param selectionArgs

         *            (Optional) Selection arguments used in the query.

         * @return The value of the _data column, which is typically a file path.

         */

        public static String getDataColumn(Context context, Uri uri,

                                           String selection, String[] selectionArgs) {


            Cursor cursor = null;

            final String column = "_data";

            final String[] projection = { column };


            try {

                cursor = context.getContentResolver().query(uri, projection,

                        selection, selectionArgs, null);

                if (cursor != null && cursor.moveToFirst()) {

                    final int index = cursor.getColumnIndexOrThrow(column);

                    return cursor.getString(index);

                }

            } finally {

                if (cursor != null)

                    cursor.close();

            }

            return nopath;

        }


        /**

         * @param uri

         * The Uri to check.

         * @return Whether the Uri authority is ExternalStorageProvider.

         */

        public static boolean isExternalStorageDocument(Uri uri) {

            return "com.android.externalstorage.documents".equals(uri

                    .getAuthority());

        }


        /**

         * @param uri

         *            The Uri to check.

         * @return Whether the Uri authority is DownloadsProvider.

         */

        public static boolean isDownloadsDocument(Uri uri) {

            return "com.android.providers.downloads.documents".equals(uri

                    .getAuthority());

        }


        /**

         * @param uri

         *            The Uri to check.

         * @return Whether the Uri authority is MediaProvider.

         */

        public static boolean isMediaDocument(Uri uri) {

            return "com.android.providers.media.documents".equals(uri

                    .getAuthority());

        }


        /**

         * @param uri

         *            The Uri to check.

         * @return Whether the Uri authority is Google Photos.

         */

        public static boolean isGooglePhotosUri(Uri uri) {

            return "com.google.android.apps.photos.content".equals(uri

                    .getAuthority());

        }

    }



       progressDialog = ProgressDialog.show(this,"포스트 중...", "", true);

            MultipartBody.Part body = null;

            if (uri != null) {

                File file = new File(ImageFilePath.getPath(this, uri));

                RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);

                body = MultipartBody.Part.createFormData("image", file.getName(), requestBody);

            }

            ServiceHelper.getInstance().postArticle(point_x , point_y,postTextString, body).enqueue(new Callback<ArticleModel>() {

                @Override

                public void onResponse(Call<ArticleModel> call, Response<ArticleModel> response) {


                   System.out.println("response success");

                    if (progressDialog != null){

                        progressDialog.dismiss();

                    }


                    finishActivity();

                }


                @Override

                public void onFailure(Call<ArticleModel> call, Throwable t) {

                    if (progressDialog != null){

                        progressDialog.dismiss();

                    }

                        Log.d("response fail: ", "");


                }

            });



    댓글 0

Designed by Tistory.