2016-12-16 5 views
2

사용자가 일부 제안을 위해 갤러리에서 그림을 선택할 수있는 기능을 구현하려고합니다. 변경 사항 (필터, 자르기 등)을 적용하기 전에이 그림을 새 그림으로 저장해야합니다.안드로이드 이미지 갤러리에서 새 파일로

지금까지 내가 그랬어 :

private void pickImageFromGallery(){ 
    /*Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("image/*"); 
    startActivityForResult(intent, GALLERY_SELECT_PICTURE);*/ 

    Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT); 
    getIntent.setType("image/*"); 

    Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    pickIntent.setType("image/*"); 

    Intent chooserIntent = Intent.createChooser(getIntent, ""); 
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent}); 

    startActivityForResult(chooserIntent, GALLERY_SELECT_PICTURE); 
} 


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

    if(resultCode == RESULT_OK){ 
     if(requestCode == GALLERY_SELECT_PICTURE){ 
      if(data == null){ 
       //TODO SHOW ERROR 
       return; 
      } 
      try { 
       Bitmap temporaryBitmap = MediaStore.Images.Media.getBitmap(myContext.getContentResolver(), data.getData()); 

       //Tried using inputStream and got the same result 
       //InputStream inputStream = myContext.getContentResolver().openInputStream(data.getData()); 
       //Bitmap temporaryBitmap = BitmapFactory.decodeStream(inputStream, null, options); 

       //Just return a file to save the bitmap into (I use the same code in different activities and it works perfectly) 
       capturedImage = FunctionUtil.getOutputMediaFile(ConstUtil.ids.MEDIA_TYPE_IMAGE); 

       FileOutputStream outputStream = new FileOutputStream(capturedImage);; 
       temporaryBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); 

       //Just refresh the gallery so my new picture becomes available (I use the same function in different activities and it works fine) 
       FunctionUtil.refreshMediaGallery(capturedImage); 

      //HERE I CHECK THE PATH/GALLERY AND NOTICE THE FILE ISN'T SAVED 

      } catch (Exception e) { 
       e.printStackTrace(); 
       //TODO SHOW ERROR 
      } 

     }else if(requestCode == GALLERY_SELECT_VIDEO){ 

     } 
    } 
} 

코드를 새로운 파일 (FunctionUtil.getOutputMediaFile), 갤러리 (FunctionUtil.refreshMediaGallery)를 새로 고침 할 수있는 코드와 비트 맵을 저장하는 코드를 얻으려면 (Bitmap.compress) 같은 활동의 다른 부분에서 잘 작동하지만 갤러리의 사진이있는 그냥 저장하지 마십시오!

카메라 API를 사용하여 새 사진을 찍은 다음 비트 맵으로 디코딩하면 완벽하게 작동하지만 갤러리에서 사진을 선택하고 비트 맵으로 디코딩하면 작동하지 않습니다.

답변

2

사용이 코드

if(requestCode == GALLERY_SELECT_PICTURE){ 
     InputStream inputStream = getContentResolver() 
      .openInputStream(data.getData()); 
     FileOutputStream fileOutputStream = new FileOutputStream(
            outputFile); 
     copyStream(inputStream, fileOutputStream); 
     fileOutputStream.close(); 
     inputStream.close(); 
} 

public static void copyStream(InputStream input, OutputStream output) 
      throws IOException { 

     byte[] buffer = new byte[1024]; 
     int bytesRead; 
     while ((bytesRead = input.read(buffer)) != -1) { 
      output.write(buffer, 0, bytesRead); 
     } 
    } 
관련 문제