마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 갤러리 이미지 서버 저장 할때 질문좀 드릴께요!

0 추천
@SuppressLint("NewApi")
public class UploadTest extends Activity {
    ProgressDialog prgDialog;
    String encodedString1 ,encodedString2;
    RequestParams params = new RequestParams();
    String imgPath1, imgPath2, fileName1, fileName2;
    Bitmap bitmap1 , bitmap2;
    private static int RESULT_LOAD_IMG1 = 1;
    private static int RESULT_LOAD_IMG2 = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.upload_test);
        prgDialog = new ProgressDialog(this);
        prgDialog.setCancelable(false);
    }

    public void loadImagefromGallery1(View view) {
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG1);
    }

    public void loadImagefromGallery2(View view) {
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        // Start the Intent
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG2);
    }

    // 갤러리에서 이미지 선택시
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            //첫번째 이미지
            if (requestCode == RESULT_LOAD_IMG1 && resultCode == RESULT_OK && null != data) {
                Uri selectedImage1 = data.getData();
                String[] filePathColumn1 = { MediaStore.Images.Media.DATA };
                Cursor cursor1 = getContentResolver().query(selectedImage1, filePathColumn1, null, null, null);
                cursor1.moveToFirst();
                int columnIndex1 = cursor1.getColumnIndex(filePathColumn1[0]);
                imgPath1 = cursor1.getString(columnIndex1);
                cursor1.close();
                ImageView imgView1 = (ImageView) findViewById(R.id.imgView1);
                imgView1.setImageBitmap(BitmapFactory.decodeFile(imgPath1));
                String fileNameSegments1[] = imgPath1.split("/");
                fileName1 = fileNameSegments1[fileNameSegments1.length - 1];

                params.put("filename1", fileName1);
            } else {
                Toast.makeText(this, "이미지를 찾을수 없습니다.",
                        Toast.LENGTH_LONG).show();
            }

            if (requestCode == RESULT_LOAD_IMG2 && resultCode == RESULT_OK && null != data) {

                Uri selectedImage2 = data.getData();
                String[] filePathColumn2 = { MediaStore.Images.Media.DATA };
                Cursor cursor2 = getContentResolver().query(selectedImage2, filePathColumn2, null, null, null);
                cursor2.moveToFirst();
                int columnIndex2 = cursor2.getColumnIndex(filePathColumn2[0]);
                imgPath2 = cursor2.getString(columnIndex2);
                cursor2.close();
                ImageView imgView2 = (ImageView) findViewById(R.id.imgView2);
                imgView2.setImageBitmap(BitmapFactory.decodeFile(imgPath2));
                String fileNameSegments2[] = imgPath2.split("/");
                fileName2 = fileNameSegments2[fileNameSegments2.length - 1];
                params.put("filename2", fileName2);

            } else {
                Toast.makeText(this, "이미지를 찾을수 없습니다.",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "문제 발생", Toast.LENGTH_LONG)
                    .show();
        }

    }
    public void uploadImage(View v) {
        if (imgPath1 != null && !imgPath1.isEmpty()) {
            prgDialog.setMessage("이진데이터로 이미지 변환");
            prgDialog.show();
            encodeImagetoString();
        } else {
            Toast.makeText(
                    getApplicationContext(),
                    "갤러리에서 이미지를 선택하세요",
                    Toast.LENGTH_LONG).show();
        }
        if (imgPath2 != null && !imgPath2.isEmpty()) {
            prgDialog.setMessage("이진데이터로 이미지 변환");
            prgDialog.show();
            encodeImagetoString();
        } else {
            Toast.makeText(
                    getApplicationContext(),
                    "갤러리에서 이미지를 선택하세요",
                    Toast.LENGTH_LONG).show();
        }
    }

    public void encodeImagetoString() {
        new AsyncTask() {

            protected void onPreExecute() {

            }

            @Override
            protected String doInBackground(Void... params) {
                BitmapFactory.Options options1 = null;
                BitmapFactory.Options options2 = null;

                options1 = new BitmapFactory.Options();
                options2 = new BitmapFactory.Options();

                options1.inSampleSize = 3;
                options2.inSampleSize = 3;

                bitmap1 = BitmapFactory.decodeFile(imgPath1, options1);
                bitmap2 = BitmapFactory.decodeFile(imgPath2, options2);

                ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
                ByteArrayOutputStream stream2 = new ByteArrayOutputStream();

                // Must compress the Image to reduce image size to make upload easy
                bitmap1.compress(Bitmap.CompressFormat.JPEG, 50, stream1);
                bitmap2.compress(Bitmap.CompressFormat.JPEG, 50, stream2);

                byte[] byte_arr1 = stream1.toByteArray();
                byte[] byte_arr2 = stream2.toByteArray();

                encodedString1 = Base64.encodeToString(byte_arr1, 0);
                encodedString2 = Base64.encodeToString(byte_arr2, 0);
                return "";
            }

            @Override
            protected void onPostExecute(String msg) {
                prgDialog.setMessage("Calling Upload");

                params.put("image1", encodedString1);
                params.put("image2", encodedString2);
                triggerImageUpload();
            }
        }.execute(null, null, null);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (prgDialog != null) {
            prgDialog.dismiss();
        }
    }
}


제가 이미지를 2개를 하나 하나씩 선택해서 이미지를 업로드 하고 싶은데요
위 소스대로 하면 이미지뷰에 똑같은 이미지가 뜨네요 ㅜㅜ
어떻게 해야 두개를 보낼수 있나요?

 

사진을보시면 알겠지만 이미지 선택 버튼은 2개인데 위에 버튼을 선택해서 이미지를 불러와도
밑에 이미지 뷰에도 똑같이 뜨네요 사진과 같이 ㅜㅜ<!--"<-->

알랙스박하순 (200 포인트) 님이 2015년 9월 22일 질문

2개의 답변

0 추천
정확하게 분석은 못했지만 리퀘스트 코드를 둘다 1로 주셨는데요! 하나는 2로 주시던가 해야할듯 합니다.
chemkaaa (6,030 포인트) 님이 2015년 9월 22일 답변
아! 감사합니다 ㅜㅜ 제가 저걸 퍼온 소스라 ㅜㅜ 이해를 못했어요.... 대충 이해는 갔는데... 감사합니다!
0 추천
아 늦었다... 코드 한참 봤는데 의외로 간단한 문제였네요.
Chemkaaa님 말이 맞습니다. 코드가 같아서 항상 1번 이미지를 선택한 것으로만 인식하니 이미지 2로써 사용되지 못하는 문제입니다.
Jinthree (8,980 포인트) 님이 2015년 9월 22일 답변
아 ㅜㅜ 감사합니다! 소스를 퍼온거라 저게 뭔가 몰라서 1개를 더 추가한거였거든요 ㅜㅜ
...