|
24 | 24 | import android.os.Bundle; |
25 | 25 | import android.os.Environment; |
26 | 26 | import android.os.Handler; |
27 | | -import android.support.annotation.NonNull; |
28 | | -import android.support.v4.app.ActivityCompat; |
29 | | -import android.support.v4.content.ContextCompat; |
30 | 27 | import android.util.Base64; |
31 | 28 | import android.util.DisplayMetrics; |
32 | 29 | import android.util.Log; |
|
46 | 43 | import java.io.ByteArrayOutputStream; |
47 | 44 | import java.io.DataInputStream; |
48 | 45 | import java.io.File; |
| 46 | +import java.io.FileInputStream; |
49 | 47 | import java.io.FileNotFoundException; |
| 48 | +import java.io.FileOutputStream; |
50 | 49 | import java.io.FileReader; |
51 | 50 | import java.io.FileWriter; |
52 | 51 | import java.io.IOException; |
| 52 | +import java.io.InputStream; |
53 | 53 | import java.io.InputStreamReader; |
| 54 | +import java.io.OutputStream; |
54 | 55 | import java.io.UnsupportedEncodingException; |
55 | 56 | import java.net.Inet4Address; |
56 | 57 | import java.net.InetAddress; |
|
72 | 73 | import java.util.concurrent.Semaphore; |
73 | 74 | import java.util.zip.GZIPInputStream; |
74 | 75 |
|
| 76 | +import androidx.annotation.NonNull; |
| 77 | +import androidx.core.app.ActivityCompat; |
| 78 | +import androidx.core.content.ContextCompat; |
| 79 | +import androidx.core.content.FileProvider; |
| 80 | + |
75 | 81 | /** |
76 | 82 | * Extends NativeActivity to provide interface methods for runtime.cpp |
77 | 83 | * |
@@ -539,7 +545,8 @@ public void share(final byte[] pathBytes) { |
539 | 545 | Intent sendIntent = new Intent(); |
540 | 546 | sendIntent.setAction(Intent.ACTION_SEND); |
541 | 547 | sendIntent.putExtra(Intent.EXTRA_TEXT, buffer); |
542 | | - sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); |
| 548 | + sendIntent.putExtra(Intent.EXTRA_STREAM, getSharedFile(file)); |
| 549 | + sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
543 | 550 | sendIntent.putExtra(Intent.EXTRA_SUBJECT, file.getName()); |
544 | 551 | sendIntent.setType("text/plain"); |
545 | 552 | startActivity(Intent.createChooser(sendIntent, "Share")); |
@@ -669,6 +676,24 @@ public void run() { |
669 | 676 | }); |
670 | 677 | } |
671 | 678 |
|
| 679 | + private void copy(File src, File dst) throws IOException { |
| 680 | + InputStream in = new FileInputStream(src); |
| 681 | + try { |
| 682 | + OutputStream out = new FileOutputStream(dst); |
| 683 | + try { |
| 684 | + byte[] buf = new byte[1024]; |
| 685 | + int len; |
| 686 | + while ((len = in.read(buf)) > 0) { |
| 687 | + out.write(buf, 0, len); |
| 688 | + } |
| 689 | + } finally { |
| 690 | + out.close(); |
| 691 | + } |
| 692 | + } finally { |
| 693 | + in.close(); |
| 694 | + } |
| 695 | + } |
| 696 | + |
672 | 697 | private String execBuffer(final String buffer, final String name, boolean run) throws IOException { |
673 | 698 | File outputFile = getApplication().getFileStreamPath(name); |
674 | 699 | BufferedWriter output = new BufferedWriter(new FileWriter(outputFile)); |
@@ -773,6 +798,23 @@ private Map<String, String> getPostData(DataInputStream inputStream, final Strin |
773 | 798 | return result; |
774 | 799 | } |
775 | 800 |
|
| 801 | + private Uri getSharedFile(File file) { |
| 802 | + Uri result; |
| 803 | + try { |
| 804 | + File sharesPath = new File(getExternalFilesDir(null), "shares"); |
| 805 | + if (sharesPath.mkdirs()) { |
| 806 | + Log.i(TAG, "created folder: " + sharesPath.toString()); |
| 807 | + } |
| 808 | + File shareFile = new File(sharesPath, file.getName()); |
| 809 | + copy(file, sharesPath); |
| 810 | + result = FileProvider.getUriForFile(this, "net.sourceforge.smallbasic.provider", shareFile); |
| 811 | + } catch (Exception e) { |
| 812 | + result = null; |
| 813 | + Log.e(TAG, "failed to create shared file", e); |
| 814 | + } |
| 815 | + return result; |
| 816 | + } |
| 817 | + |
776 | 818 | private String getString(final byte[] promptBytes) { |
777 | 819 | try { |
778 | 820 | return new String(promptBytes, CP1252); |
|
0 commit comments