Skip to content

Commit 13a7404

Browse files
committed
modernise sharing - now using File Provider.
1 parent df897e0 commit 13a7404

7 files changed

Lines changed: 75 additions & 71 deletions

File tree

app/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
apply plugin: 'com.android.application'
2-
32
android {
43
compileSdkVersion 19
54
buildToolsVersion "23.0.2"
@@ -20,3 +19,7 @@ android {
2019
}
2120
}
2221
}
22+
23+
dependencies {
24+
compile 'com.android.support:support-v4:23.3.0'
25+
}

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,14 @@
2727
android:name=".SKNPrefs"
2828
android:label="@string/preferences_title">
2929
</activity>
30+
<provider
31+
android:name="android.support.v4.content.FileProvider"
32+
android:authorities="com.manichord.sketchnotes.fileprovider"
33+
android:grantUriPermissions="true"
34+
android:exported="false">
35+
<meta-data
36+
android:name="android.support.FILE_PROVIDER_PATHS"
37+
android:resource="@xml/filepaths" />
38+
</provider>
3039
</application>
3140
</manifest>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.manichord.sketchnotes;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
9+
/**
10+
* Misc fiel related util methods.
11+
*/
12+
public class FileUtils {
13+
14+
/**
15+
* Copies src file to dst file. If the dst file does not exist, it will be
16+
* created.
17+
*
18+
* @param src
19+
* @param dst
20+
* @throws IOException
21+
*/
22+
public static void copyToFile(InputStream src, File dst)
23+
throws IOException {
24+
OutputStream out = null;
25+
try {
26+
out = new FileOutputStream(dst);
27+
28+
// Transfer bytes from in to out
29+
byte[] buf = new byte[1024];
30+
int len;
31+
while ((len = src.read(buf)) > 0) {
32+
out.write(buf, 0, len);
33+
}
34+
} finally {
35+
try {
36+
if (out != null) {
37+
out.close();
38+
}
39+
} catch (IOException e) {
40+
// can't do anything about this
41+
}
42+
}
43+
}
44+
}

app/src/main/java/com/manichord/sketchnotes/PageImageAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public MkThumbsAsync(ImageView imgView) {
130130
protected Bitmap doInBackground(Integer... params) {
131131
mPosition = params[0];
132132
File currentfile = mFileList[mPosition];
133-
Log.e(TAG, "img file:"+currentfile);
133+
Log.d(TAG, "img file:"+currentfile);
134134
//for explanation of why to use BMFactory options see
135135
//http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966
136136

app/src/main/java/com/manichord/sketchnotes/SKNotes.java

Lines changed: 13 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package com.manichord.sketchnotes;
22

3-
import java.io.File;
4-
import java.io.FileInputStream;
5-
import java.io.FileOutputStream;
6-
import java.io.IOException;
7-
import java.io.InputStream;
8-
import java.io.OutputStream;
9-
import java.util.Date;
10-
113
import android.app.Activity;
124
import android.content.Context;
135
import android.content.Intent;
146
import android.net.Uri;
157
import android.os.Bundle;
168
import android.os.Debug;
9+
import android.support.v4.content.FileProvider;
1710
import android.util.Log;
1811
import android.view.LayoutInflater;
1912
import android.view.Menu;
@@ -26,6 +19,11 @@
2619
import android.widget.TableRow;
2720
import android.widget.Toast;
2821

22+
import java.io.File;
23+
import java.io.FileInputStream;
24+
import java.io.IOException;
25+
import java.util.Date;
26+
2927
public class SKNotes extends Activity {
3028

3129
private static final String TAG = "SKNotes";
@@ -105,67 +103,14 @@ private void shareSketch() {
105103
// make sure current version is saved
106104
sView.saveCurrentBitMap(mCurrentFileName);
107105
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
108-
File extFile = new File(getShareDir(), mCurrentFileName);
109-
FileInputStream internalFile = null;
110-
try {
111-
internalFile = getApplicationContext().openFileInput(
112-
mCurrentFileName);
113-
copyToFile(internalFile, extFile);
114-
Uri sketchUri = Uri.fromFile(extFile);
115-
sharingIntent.setType("image/png");
116-
sharingIntent.putExtra(Intent.EXTRA_STREAM, sketchUri);
117-
startActivity(Intent.createChooser(sharingIntent,
118-
"Share Sketch Using..."));
119-
} catch (IOException e) {
120-
Log.e(TAG, "error sharing " + mCurrentFileName, e);
121-
} finally {
122-
if (internalFile != null) {
123-
try {
124-
internalFile.close();
125-
} catch (IOException e) {
126-
// can't do anything about this
127-
}
128-
}
129-
}
130-
}
131-
132-
private File getShareDir() {
133-
File dir = new File(getExternalCacheDir(), "share");
134-
if (!dir.exists()) {
135-
dir.mkdirs();
136-
}
137-
return dir;
138-
}
139106

140-
/**
141-
* Copies src file to dst file. If the dst file does not exist, it will be
142-
* created.
143-
*
144-
* @param src
145-
* @param dst
146-
* @throws IOException
147-
*/
148-
public static void copyToFile(InputStream src, File dst)
149-
throws IOException {
150-
OutputStream out = null;
151-
try {
152-
out = new FileOutputStream(dst);
153-
154-
// Transfer bytes from in to out
155-
byte[] buf = new byte[1024];
156-
int len;
157-
while ((len = src.read(buf)) > 0) {
158-
out.write(buf, 0, len);
159-
}
160-
} finally {
161-
try {
162-
if (out != null) {
163-
out.close();
164-
}
165-
} catch (IOException e) {
166-
// can't do anything about this
167-
}
168-
}
107+
Uri sketchUri = FileProvider.getUriForFile(getApplicationContext(),
108+
"com.manichord.sketchnotes.fileprovider",
109+
new File(getApplicationContext().getFilesDir(),mCurrentFileName));
110+
sharingIntent.setType("image/png");
111+
sharingIntent.putExtra(Intent.EXTRA_STREAM, sketchUri);
112+
startActivity(Intent.createChooser(sharingIntent,
113+
"Share Sketch Using..."));
169114
}
170115

171116
@Override

app/src/main/java/com/manichord/sketchnotes/SketchView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public void saveCurrentBitMap(String filename) {
288288
return; //do nothing if no unsaved changes pending
289289
}
290290
try {
291-
FileOutputStream out = ((Activity)getContext()).openFileOutput(filename,
291+
FileOutputStream out = (getContext()).openFileOutput(filename,
292292
Context.MODE_PRIVATE);
293293
mBitmap.compress(Bitmap.CompressFormat.PNG, 99, out); //note PNG lossless
294294
mUnsaved = false;

app/src/main/res/xml/filepaths.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<paths>
2+
<files-path path="/" name="sketch-images" />
3+
</paths>

0 commit comments

Comments
 (0)