2014-02-13 13 views
7

한 번에 두 개 이상의 이미지를 업로드 할 수있는 이미지 업 로더를 만들고 있습니다.Android - 어떻게 이미지를 ClipData에서 가져올 수 있습니까?

galerijButton.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
     photoPickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 

     photoPickerIntent.setType("image/*"); 
     startActivityForResult(photoPickerIntent, SELECT_PHOTO); 
    } 
}); 

... 내가 ArrayList에 내 사진을 배열에 선택한 모든 이미지를 추가 할

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO: 
     if(resultCode == RESULT_OK){ 
      if(imageReturnedIntent.getData() != null){ 
       //If uploaded with Android Gallery (max 1 image) 
       Uri selectedImage = imageReturnedIntent.getData(); 
       InputStream imageStream; 
       try { 
        imageStream = getContentResolver().openInputStream(selectedImage); 
        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream); 
        photos.add(yourSelectedImage); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 
      } else { 
       //If uploaded with the new Android Photos gallery 
       ClipData clipData = imageReturnedIntent.getClipData(); 
       for(int i = 0; i < clipData.getItemCount(); i++){ 
        clipData.getItemAt(i); 
        //What now? 
       } 
      } 
     } 
    break; 
    .... 

. 어떻게 든 ClipData.Item 비트 맵으로 변환해야하지만 어떻게?

+0

클립 보드에서 이미지를 복사하려고합니까? – SMR

+0

아니요, Android 사진 앱을 사용하여 한 번에 여러 이미지를 업로드하고 있습니다. Android 포토 앱은 선택한 이미지를 (의도) imageReturnedIntent.getClipData(); 그 이유는 Array의 크기가 항상 선택된 이미지의 수이기 때문입니다. –

답변

9

ClipData.Item item = clip.getItemAt(i); 
Uri uri = item.getUri(); 

지금 당신이 의 URI를 이미지의을 시도합니다.

이제 어떻게해야 할 지 알 것 같습니다. 건배 :)

+0

예, 감사합니다! 그것은 작동합니다. –

0

문자열로 이미지를 인코딩하면 이미지를 다시 디코딩 할 때 사용할 수 있습니다.

이미지 목록의 경우 문자열 배열을 만들어 그 이미지를 저장하십시오.

public static String encodeTobase64(Bitmap image) { 
    Bitmap immage = image; 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    immage.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] b = baos.toByteArray(); 
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT); 
    Log.d("Image Log:", imageEncoded); 
    return imageEncoded; 
} 

public static Bitmap decodeBase64(String input) { 
    try { 
     byte[] encodeByte = Base64.decode(input, Base64.DEFAULT); 
     InputStream is = new ByteArrayInputStream(encodeByte); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Config.ALPHA_8; 
     options.inSampleSize = 2; 
     Bitmap bitmap = BitmapFactory.decodeStream(is, null, options); 
     return bitmap; 
    } catch (Exception e) { 
     e.getMessage(); 
     return null; 
    } 
} 
+0

감사합니다. 시험해 보겠습니다. –