Skip to content

Instantly share code, notes, and snippets.

@hjiee
Created October 29, 2019 08:37
Show Gist options
  • Save hjiee/177d64c11f59ee05bbdab7baaa5600ad to your computer and use it in GitHub Desktop.
Save hjiee/177d64c11f59ee05bbdab7baaa5600ad to your computer and use it in GitHub Desktop.
private void afterQ() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/*");
// 파일을 write중이라면 다른곳에서 데이터요구를 무시하겠다는 의미입니다.
values.put(MediaStore.Images.Media.IS_PENDING, 1);
ContentResolver contentResolver = getContentResolver();
Uri collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri item = contentResolver.insert(collection, values);
try {
ParcelFileDescriptor pdf = contentResolver.openFileDescriptor(item, "w", null);
if (pdf == null) {
} else {
InputStream inputStream = getImageInputStram();
byte[] strToByte = getBytes(inputStream);
FileOutputStream fos = new FileOutputStream(pdf.getFileDescriptor());
fos.write(strToByte);
fos.close();
inputStream.close();
pdf.close();
contentResolver.update(item, values, null, null);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
values.clear();
// 파일을 모두 write하고 다른곳에서 사용할 수 있도록 0으로 업데이트를 해줍니다.
values.put(MediaStore.Images.Media.IS_PENDING, 0);
contentResolver.update(item, values, null, null);
}
private InputStream getImageInputStram() {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bytes);
byte[] bitmapData = bytes.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapData);
return bs;
}
public byte[] getBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment