Skip to content

Commit bb76c29

Browse files
committed
Added missing Runtime Permission for API >=23
Update README.md
1 parent 460aa2b commit bb76c29

File tree

2 files changed

+73
-14
lines changed

2 files changed

+73
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Images To PDF
22

33
### Badges
4-
[![Build Status](https://travis-ci.org/Swati4star/Images-to-PDF.svg?branch=master)](https://travis-ci.org/Swati4star/Images-to-PDF)
4+
[![Build Status](https://travis-ci.org/Swati4star/Images-to-PDF.svg?branch=master)](https://travis-ci.org/Swati4star/Images-to-PDF) [![Circle CI](https://circleci.com/gh/Swati4star/Images-to-PDF.svg?style=svg)](https://circleci.com/gh/Swati4star/Images-to-PDF)
55

66
Have JPG when you need a PDF? Convert JPG to PDF in a few seconds! :smiley:
77
Here is an easy to use android app to convert images to PDF file!

app/src/main/java/swati4star/createpdf/First.java

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package swati4star.createpdf;
22

3+
import android.Manifest;
34
import android.app.Activity;
45
import android.content.ActivityNotFoundException;
56
import android.content.Context;
67
import android.content.Intent;
8+
import android.content.pm.PackageManager;
79
import android.graphics.Bitmap;
810
import android.graphics.BitmapFactory;
911
import android.net.Uri;
1012
import android.os.AsyncTask;
13+
import android.os.Build;
1114
import android.os.Bundle;
1215
import android.os.Environment;
1316
import android.support.annotation.ColorRes;
1417
import android.support.annotation.DimenRes;
1518
import android.support.annotation.IntegerRes;
1619
import android.support.v4.app.Fragment;
20+
import android.support.v4.content.ContextCompat;
1721
import android.util.Log;
1822
import android.view.LayoutInflater;
1923
import 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

Comments
 (0)