|
1 | 1 | package com.manichord.sketchnotes; |
2 | 2 |
|
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 | | - |
11 | 3 | import android.app.Activity; |
12 | 4 | import android.content.Context; |
13 | 5 | import android.content.Intent; |
14 | 6 | import android.net.Uri; |
15 | 7 | import android.os.Bundle; |
16 | 8 | import android.os.Debug; |
| 9 | +import android.support.v4.content.FileProvider; |
17 | 10 | import android.util.Log; |
18 | 11 | import android.view.LayoutInflater; |
19 | 12 | import android.view.Menu; |
|
26 | 19 | import android.widget.TableRow; |
27 | 20 | import android.widget.Toast; |
28 | 21 |
|
| 22 | +import java.io.File; |
| 23 | +import java.io.FileInputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.Date; |
| 26 | + |
29 | 27 | public class SKNotes extends Activity { |
30 | 28 |
|
31 | 29 | private static final String TAG = "SKNotes"; |
@@ -105,67 +103,14 @@ private void shareSketch() { |
105 | 103 | // make sure current version is saved |
106 | 104 | sView.saveCurrentBitMap(mCurrentFileName); |
107 | 105 | 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 | | - } |
139 | 106 |
|
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...")); |
169 | 114 | } |
170 | 115 |
|
171 | 116 | @Override |
|
0 commit comments