1
1
package fr .free .nrw .commons ;
2
2
3
+ import android .content .Intent ;
4
+ import android .graphics .Bitmap ;
5
+ import android .net .Uri ;
6
+ import android .os .Environment ;
3
7
import android .support .v7 .app .AppCompatActivity ;
4
8
import android .os .Bundle ;
5
9
import android .util .DisplayMetrics ;
10
+ import android .util .Log ;
6
11
import android .view .Menu ;
7
12
import android .view .MenuInflater ;
13
+ import android .view .MenuItem ;
8
14
import android .view .View ;
9
15
import android .widget .ImageView ;
10
16
import android .widget .LinearLayout ;
11
17
import android .widget .RelativeLayout ;
18
+ import android .widget .Toast ;
12
19
import android .widget .Toolbar ;
13
20
21
+ import java .io .File ;
22
+ import java .io .FileOutputStream ;
23
+ import java .util .Random ;
24
+
14
25
import fr .free .nrw .commons .theme .NavigationBaseActivity ;
15
26
16
27
public class Achievements extends AppCompatActivity {
17
28
18
29
private static final double badge_image_ratio_width = 0.5 ;
19
30
private static final double badge_image_ratio_height = 0.5 ;
20
-
31
+ private ImageView imageView ;
21
32
@ Override
22
33
protected void onCreate (Bundle savedInstanceState ) {
23
34
super .onCreate (savedInstanceState );
@@ -28,7 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
28
39
int height = displayMetrics .heightPixels ;
29
40
int width = displayMetrics .widthPixels ;
30
41
31
- ImageView imageView = (ImageView )findViewById (R .id .achievement_badge );
42
+ imageView = (ImageView )findViewById (R .id .achievement_badge );
32
43
33
44
RelativeLayout .LayoutParams params = (RelativeLayout .LayoutParams )
34
45
imageView .getLayoutParams ();
@@ -38,11 +49,56 @@ protected void onCreate(Bundle savedInstanceState) {
38
49
imageView .requestLayout ();
39
50
40
51
android .support .v7 .widget .Toolbar toolbar = (android .support .v7 .widget .Toolbar ) findViewById (R .id .toolbar );
41
- toolbar .inflateMenu (R .menu .menu_about );
52
+ setSupportActionBar (toolbar );
53
+
54
+ }
55
+
56
+ @ Override
57
+ public boolean onCreateOptionsMenu (Menu menu ) {
58
+ // Inflate the menu; this adds items to the action bar if it is present.
59
+ getMenuInflater ().inflate (R .menu .menu_about , menu );
60
+ return true ;
61
+ }
62
+
63
+ @ Override
64
+ public boolean onOptionsItemSelected (MenuItem item ) {
65
+ int id = item .getItemId ();
66
+ if (id == R .id .share_app_icon ) {
67
+ View rootView = getWindow ().getDecorView ().findViewById (android .R .id .content );
68
+ Bitmap screenShot = getScreenShot (rootView );
69
+ imageView .setImageBitmap (screenShot );
70
+ shareScreen (screenShot );
71
+ }
72
+
73
+ return super .onOptionsItemSelected (item );
74
+ }
75
+
76
+ public static Bitmap getScreenShot (View view ) {
77
+ View screenView = view .getRootView ();
78
+ screenView .setDrawingCacheEnabled (true );
79
+ Bitmap bitmap = Bitmap .createBitmap (screenView .getDrawingCache ());
80
+ screenView .setDrawingCacheEnabled (false );
81
+ return bitmap ;
82
+ }
42
83
84
+ void shareScreen ( Bitmap bitmap ){
85
+ try {
86
+ File file = new File (this .getExternalCacheDir (),"screen.png" );
87
+ FileOutputStream fOut = new FileOutputStream (file );
88
+ bitmap .compress (Bitmap .CompressFormat .PNG , 100 , fOut );
89
+ fOut .flush ();
90
+ fOut .close ();
91
+ file .setReadable (true , false );
92
+ final Intent intent = new Intent (android .content .Intent .ACTION_SEND );
93
+ intent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
94
+ intent .putExtra (Intent .EXTRA_STREAM , Uri .fromFile (file ));
95
+ intent .setType ("image/png" );
96
+ startActivity (Intent .createChooser (intent , "Share image via" ));
97
+ } catch (Exception e ) {
98
+ e .printStackTrace ();
99
+ }
43
100
}
44
101
45
-
46
102
47
103
48
104
}
0 commit comments