하나의 화면 자체를 공유하고 싶을때 사용합니다.
작동 원리는 레이아웃을 스크린샷 형태로 갤러리에 저장한 뒤에 그 uri을 가져와서 공유하는 방식입니다.
일단 LayoutToImage 클래스를 복붙 합니다!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
public class LayoutToImage {
public interface SaveImageCallback{
void imageResult(int status, Uri uri);
}
SaveImageCallback saveImageCallback;
public void setSaveImageCallback(SaveImageCallback saveImageCallback) {
this.saveImageCallback = saveImageCallback;
}
public void saveBitMap(Context context, View drawView) {
File pictureFileDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), context.getString(R.string.app_name));
if (!isDirectoryCreated) {
if(saveImageCallback != null){
saveImageCallback.imageResult(400, null);
}
return;
}
}
String filename = pictureFileDir.getPath() + File.separator + System.currentTimeMillis() + ".jpg";
File pictureFile = new File(filename);
Bitmap bitmap = getBitmapFromView(drawView);
try {
pictureFile.createNewFile();
FileOutputStream oStream = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, oStream);
} catch (IOException e) {
if(saveImageCallback != null){
saveImageCallback.imageResult(400, null);
}
}
scanGallery(context, pictureFile.getAbsolutePath());
}
//create bitmap from view and returns it
private Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
//has background drawable, then draw it on the canvas
} else {
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
}
// draw the view on the canvas
//return the bitmap
return returnedBitmap;
}
// used for scanning gallery
private void scanGallery(Context context, String path) {
try {
MediaScannerConnection.scanFile(context, new String[]{path}, null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
if(saveImageCallback != null){
saveImageCallback.imageResult(200, uri);
}
}
});
} catch (Exception e) {
if(saveImageCallback != null){
saveImageCallback.imageResult(400, null);
}
}
}
}
|
사용할 액티비티에서 임플리먼트를 해주신 다음에 sharedImage 함수를 만들고 콜백 함수에 담아줍니다.
1
|
implements LayoutToImage.SaveImageCallback
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@Override
public void imageResult(int status, Uri uri) {
switch (status) {
case 200:
shareIamge(uri);
break;
case 400:
break;
}
}
private void shareIamge(Uri uri) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(intent.createChooser(intent, "Share Image"));
}
|
마지막으로 사용할 곳에서 적어주시면 끄읕!
저는 카드뷰 레이아웃을 공유하였습니다.
1
2
3
|
LayoutToImage layoutToImage = new LayoutToImage();
layoutToImage.setSaveImageCallback(this);
layoutToImage.saveBitMap(this, cardView);
|
'개발 > ANDROID' 카테고리의 다른 글
[Android] 코루틴 기초 (0) | 2020.10.07 |
---|---|
[Android] 코루틴 왜 써야하는가 (0) | 2020.10.05 |
[Android] 에러 Android resource compilation failed (0) | 2019.05.30 |
[Android] RecyclerView, ListView Adapter에서 Activity finish 하기 (0) | 2019.03.06 |
[Android] 한문장으로 모든 액티비티 종료시키기 (0) | 2018.06.12 |
댓글