2017-01-02 2 views
-1

Android Studio로 카메라를 만들었으며 촬영 한 이미지를 갤러리에 저장하려고합니다.
나는 Camera2 Api을 사용하며 사진을 저장하는 방법을 모른다.
또한, 내 사진이 저장된 곳을 모르겠습니다. 응용 프로그램 : Saved: /storage/emulated/1.jpg. 내가 더 많은 사진을 저장하는 방법을 모르는,안드로이드에서 이미지를 갤러리에 저장하는 방법은 무엇입니까?

mFile = new File(Environment.getExternalStorageDirectory() + "1.jpg"); 

private final ImageReader.OnImageAvailableListener mOnImageAvailableListener 
     = new ImageReader.OnImageAvailableListener() { 

    @Override 
    public void onImageAvailable(ImageReader reader) { 
     mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage(), mFile)); 
    } 

}; 

private static class ImageSaver implements Runnable { 

    /** 
    * The JPEG image 
    */ 
    private final Image mImage; 
    /** 
    * The file we save the image into. 
    */ 
    private final File mFile; 

    public ImageSaver(Image image, File file) { 
     mImage = image; 
     mFile = file; 
    } 

    @Override 
    public void run() { 
     ByteBuffer buffer = mImage.getPlanes()[0].getBuffer(); 
     byte[] bytes = new byte[buffer.remaining()]; 
     buffer.get(bytes); 
     FileOutputStream output = null; 
     try { 
      output = new FileOutputStream(mFile); 
      output.write(bytes); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      mImage.close(); 
      if (null != output) { 
       try { 
        output.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 

} 

다음 문제는 다음과 같습니다

몇 가지 코드입니다. 이 경우 1.jpg은 항상 덮어 씁니다.

답변

0

갤러리에 사진을 추가하려면 :

private void galleryAddPic() { 
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    File f = new File(mCurrentPhotoPath); 
    Uri contentUri = Uri.fromFile(f); 
    mediaScanIntent.setData(contentUri); 
    this.sendBroadcast(mediaScanIntent); 
} 

See here

은 여러 장의 사진이 모든 새로운 사진의 새 이름을 생성

(날짜와 시간 또는 UUID를 사용) 저장하려면
관련 문제