11package swati4star .createpdf ;
22
3+ import android .Manifest ;
34import android .app .Activity ;
45import android .content .ActivityNotFoundException ;
56import android .content .Context ;
67import android .content .Intent ;
8+ import android .content .pm .PackageManager ;
79import android .graphics .Bitmap ;
810import android .graphics .BitmapFactory ;
911import android .net .Uri ;
1012import android .os .AsyncTask ;
13+ import android .os .Build ;
1114import android .os .Bundle ;
1215import android .os .Environment ;
1316import android .support .annotation .ColorRes ;
1417import android .support .annotation .DimenRes ;
1518import android .support .annotation .IntegerRes ;
1619import android .support .v4 .app .Fragment ;
20+ import android .support .v4 .content .ContextCompat ;
1721import android .util .Log ;
1822import android .view .LayoutInflater ;
1923import android .view .View ;
@@ -40,6 +44,7 @@ public class First extends Fragment {
4044
4145
4246 private static final int INTENT_REQUEST_GET_IMAGES = 13 ;
47+ private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE_RESULT = 1 ;
4348 private int mMorphCounter1 = 1 ;
4449
4550 List <String > imagesuri ;
@@ -51,6 +56,7 @@ public class First extends Fragment {
5156 String filename ;
5257 MorphingButton buttt ;
5358 Image image ;
59+ Boolean permissionsGranted = false ;
5460
5561 @ Override
5662 public void onAttach (Context context ) {
@@ -59,9 +65,9 @@ public void onAttach(Context context) {
5965 }
6066
6167 @ Override
62- public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
68+ public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
6369
64- View root = inflater .inflate (R .layout .fragment_first , container ,false );
70+ View root = inflater .inflate (R .layout .fragment_first , container , false );
6571
6672 //initialising variables
6773 imagesuri = new ArrayList <>();
@@ -71,6 +77,19 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle s
7177 btnMorph1 = (MorphingButton ) root .findViewById (R .id .pdfcreate );
7278 buttt = btnMorph1 ;
7379
80+
81+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
82+ if (ContextCompat .checkSelfPermission (ac ,
83+ Manifest .permission .WRITE_EXTERNAL_STORAGE )
84+ != PackageManager .PERMISSION_GRANTED ) {
85+ requestPermissions (new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE ,
86+ Manifest .permission .READ_EXTERNAL_STORAGE ,
87+ Manifest .permission .CAMERA },
88+ PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE_RESULT );
89+ }
90+ }
91+
92+
7493 b .setOnClickListener (new View .OnClickListener () {
7594 @ Override
7695 public void onClick (View v ) {
@@ -93,32 +112,46 @@ public void onClick(View v) {
93112 @ Override
94113 public void onClick (View v ) {
95114
96- Intent intent = new Intent (ac , ImagePickerActivity .class );
97- startActivityForResult (intent , INTENT_REQUEST_GET_IMAGES );
98-
115+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
116+ if (ContextCompat .checkSelfPermission (ac ,
117+ Manifest .permission .WRITE_EXTERNAL_STORAGE )
118+ != PackageManager .PERMISSION_GRANTED ) {
119+ requestPermissions (new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE ,
120+ Manifest .permission .READ_EXTERNAL_STORAGE ,
121+ Manifest .permission .CAMERA },
122+ PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE_RESULT );
123+ } else {
124+ selectImages ();
125+ }
126+ } else {
127+ selectImages ();
128+ }
99129 }
100130 });
101131
102132
103- morphToSquare (buttt ,integer (R .integer .mb_animation ));
133+ morphToSquare (buttt , integer (R .integer .mb_animation ));
104134 b .setVisibility (View .GONE );
105135 btnMorph1 .setOnClickListener (new View .OnClickListener () {
106136 @ Override
107137 public void onClick (View view ) {
108- if (imagesuri .size () == 0 ) {
138+ if (!permissionsGranted ) {
139+ Toast .makeText (ac , "Insufficient Permissions!" , Toast .LENGTH_LONG )
140+ .show ();
141+ } else if (imagesuri .size () == 0 ) {
109142 Toast .makeText (ac , "No Images selected" , Toast .LENGTH_LONG ).show ();
110143
111144 } else {
112145 new MaterialDialog .Builder (ac )
113146 .title ("Creating PDF" )
114147 .content ("Enter file name" )
115- .input ("Example : abc" ,null , new MaterialDialog .InputCallback () {
148+ .input ("Example : abc" , null , new MaterialDialog .InputCallback () {
116149 @ Override
117150 public void onInput (MaterialDialog dialog , CharSequence input ) {
118- if (input ==null ){
151+ if (input == null ) {
119152 Toast .makeText (ac , "Name cannot be blank" , Toast .LENGTH_LONG ).show ();
120153
121- }else {
154+ } else {
122155 filename = input .toString ();
123156 imcreate ();
124157 onMorphButton1Clicked (btnMorph1 );
@@ -136,6 +169,33 @@ public void onInput(MaterialDialog dialog, CharSequence input) {
136169 }
137170
138171
172+ public void selectImages () {
173+ Intent intent = new Intent (ac , ImagePickerActivity .class );
174+ startActivityForResult (intent , INTENT_REQUEST_GET_IMAGES );
175+
176+ }
177+
178+
179+ @ Override
180+ public void onRequestPermissionsResult (int requestCode ,
181+ String permissions [], int [] grantResults ) {
182+ switch (requestCode ) {
183+ case PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE_RESULT : {
184+ if (grantResults .length > 0
185+ && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
186+ selectImages ();
187+ Toast .makeText (ac , "Permissions Given!" , Toast .LENGTH_LONG )
188+ .show ();
189+
190+ } else {
191+ Toast .makeText (ac , "Insufficient Permissions!" , Toast .LENGTH_LONG )
192+ .show ();
193+ }
194+ return ;
195+ }
196+ }
197+ }
198+
139199 @ Override
140200 public void onActivityResult (int requestCode , int resultCode , Intent data ) {
141201 super .onActivityResult (requestCode , resultCode , data );
@@ -152,7 +212,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
152212 }
153213
154214
155-
156215 private void onMorphButton1Clicked (final MorphingButton btnMorph ) {
157216 if (mMorphCounter1 == 0 ) {
158217 mMorphCounter1 ++;
@@ -232,9 +291,9 @@ protected String doInBackground(String... params) {
232291
233292 File file = new File (Environment .getExternalStorageDirectory ().getAbsolutePath () + "/PDFfiles/" );
234293
235- path = path + filename +
294+ path = path + filename +
236295 ".pdf" ;
237- File f = new File (file , filename +
296+ File f = new File (file , filename +
238297 ".pdf" );
239298
240299 Log .v ("stage 1" , "store the pdf in sd card" );
0 commit comments