2017-12-27 29 views
1

이미지 편집기 앱을 개발 중이므로 사용자가 이미지를 저장할 때마다 그렇게합니다. 그래서 우선은이미지가 동일한 이름으로 덮어 쓰지 않습니다

String savedImageURL = MediaStore.Images.Media.insertImage(
         getContentResolver(), 
         bitmap, 
         "Bird", 
         "Image of bird" 
       ); 

이 코드를 삽입하지만, 새로운 파일을 생성하는 대신 덮어 쓰기.

그래서 내가 다른 방법

public String saveImage(String folderName, String imageName) { 
String selectedOutputPath = ""; 
     if (isSDCARDMounted()) { 
      File mediaStorageDir = new File(
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), folderName); 
      // Create a storage directory if it does not exist 
      if (!mediaStorageDir.exists()) { 
       if (!mediaStorageDir.mkdirs()) { 
        Log.d("PhotoEditorSDK", "Failed to create directory"); 
       } 
      } 
      // Create a media file name 
      selectedOutputPath = mediaStorageDir.getPath() + File.separator + imageName; 
      Log.d("PhotoEditorSDK", "selected camera path " + selectedOutputPath); 
    File file = new File(selectedOutputPath); 
    try { 
        FileOutputStream out = new FileOutputStream(file,true); 
        if (parentView != null) { 
         parentView.setDrawingCacheEnabled(true); 
         parentView.getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 80, out); 
        } 
        out.flush(); 
        out.close(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
    } 
    return selectedOutputPath; 
     } 

를 사용하지만, 그것은 또한 작동하지 않았다.

누구나 같은 이름으로 비트 맵을 덮어 쓴다는 것을 알고 있습니까? 둘째 인수로 거짓

+0

예외가 발생합니까 아니면 새 파일입니까? –

+0

어떤 오류가 발생합니까? –

+0

나는 모든 오류가 발생하지 않았다. 모든 것이 잘 작동하지만 이미지는 같은 이름이나 파일로 덮어 쓰지 않는다. 첫 번째 저장된 섹션 만 파일에 덮어 쓰지 않는 두 번째 섹션을 삽입합니다. – Dithesh

답변

1

패스, 기존 파일 덮어 쓸 수 있도록 false로 추가 설정 :

FileOutputStream out = new FileOutputStream(file,false); 

체크 아웃 생성자 documentation :

: 여기

은 당신의 코드

public String saveImage(String folderName, String imageName) { 
String selectedOutputPath = ""; 
    if (isSDCARDMounted()) { 
     File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), folderName); 
     // Create a storage directory if it does not exist 
     if (!mediaStorageDir.exists()) { 
      if (!mediaStorageDir.mkdirs()) { 
       Log.d("PhotoEditorSDK", "Failed to create directory"); 
      } 
     } 
     // Create a media file name 
     selectedOutputPath = mediaStorageDir.getPath() + File.separator + imageName; 
     Log.d("PhotoEditorSDK", "selected camera path " + selectedOutputPath); 
File file = new File(selectedOutputPath); 

if (file.exists()) 
{ 
    try { 
      file.delete(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 


try { 
file.createNewFile(); 
       FileOutputStream out = new FileOutputStream(file,false); 
       if (parentView != null) { 
        parentView.setDrawingCacheEnabled(true); 
        parentView.getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 80, out); 
       } 
       out.flush(); 
       out.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
} 
return selectedOutputPath; 
    } 
+0

나는 그 일을했지만 아직 첫 번째 저장된 이미지를 보여줍니다. – Dithesh

+0

파일을 매번 교체하고 파일을 삭제하고 새 파일을 만들려는 경우. –

+0

해당 코드 또는 링크를 추가 할 수 있습니다. 제발 – Dithesh