Bitmap bitmap = decodeFile(file);
Uri uri = shareFile(bitmap);
share(uri);
public Uri shareFile(Bitmap bitmap) {
Date now = new Date();
String fileName = "android_share_" + now + ".jpg";
Uri imageUri=null;
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
int quality = 100;
OutputStream fos;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = getActivity().getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
imageUri = resolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
} else {
String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
File image = new File(imagesDir, fileName);
fos = new FileOutputStream(image);
imageUri = Uri.fromFile(image);
}
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageUri;
}
private void share(Uri uri) {
try {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
// Set the image file URI
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
// Optional: Add a subject and text to the sharing content
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "이미지 공유");
// Grant permission for receiving apps to read the content URI
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Start the intent
startActivity(Intent.createChooser(shareIntent, "이미지 공유"));
}catch (Exception e){
Log.d("myLog", " share error " + e.toString());
}
}
'안드로이드' 카테고리의 다른 글
Android Java 갤러리 다중이미지 가져와서 연속크랍(Crop) (0) | 2024.01.12 |
---|---|
PDF 파일 불러와서 Sign하기(JAVA) (0) | 2024.01.08 |
android 10 이상 스크린 캡쳐 후 파일저장(screen capture view java) (0) | 2023.11.16 |
rxjava 파일 다운로드 (0) | 2023.11.13 |
코틀린 apply, run, let (0) | 2023.11.10 |