1

i cannot understand about sharing internal txt file. (not external !) I noticed that is not possibile by default, but i writed my ContetProvider class

< provider android:name="myProvider" android:authorities="com.mobilemerit.usbhost" android:exported="true" />

public class myProvider extends ContentProvider {
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {       
     File cacheDir = getContext().getCacheDir();
     File privateFile = new File(cacheDir, "file.txt");

     return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_WRITE);
}

then

 try {
            InputStream is = c.getAssets().open("file.txt");

            File cacheDir = c.getCacheDir();
            File outFile = new File(cacheDir, "file.txt");

            OutputStream os = new FileOutputStream(outFile.getAbsolutePath());


            String content = "Hello Java Code Geeks";
            byte[] buff = new byte[1024];
            int len;
            while ((len = is.read(buff)) > 0) {
                os.write(buff, 0, len);
            }

            os.flush();
            os.close();
            is.close();

        } catch (IOException e) {
            e.printStackTrace(); // TODO: should close streams properly here
        }


     Uri uri = Uri.parse("content://com.mobilemerit.usbhost/file.txt");
        InputStream is = null;          
        StringBuilder result = new StringBuilder();
        try {
            is = c.getContentResolver().openInputStream(uri);
            BufferedReader r = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = r.readLine()) != null) {

                result.append(line);
            }               
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try { if (is != null) is.close(); } catch (IOException e) { }
        }

but the new sharing intent that i'm writing is attaching a file.txt that is not impossible to send. It seem the attachment "it's not valid". What am i wronging? I cannot use external memory

Intent intent = new Intent(android.content.Intent.ACTION_SEND);
             intent.setType("text/plain");
             intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://com.mobilemerit.usbhost/file.txt"));
             startActivity(Intent.createChooser(intent, ""));
  • `then`. Please explain your code foolowing `then`. – greenapps May 27 '15 at 16:19
  • Then it was intend to insert another piece of code – Alessio Di Marco May 28 '15 at 12:30
  • You may find some interesting information in http://stackoverflow.com/questions/6072895/email-from-internal-storage more so in the later answers there which cover the Content Provider mechanism than in my own posting on the quirks of the direct file method which that question had first tried. – Chris Stratton May 29 '15 at 15:01

1 Answers1

0

If the only intention is to share/email an internal file, it isn't necessary to create a content provider. You can use setReadable(1) method to let every process access the file. Sample below.

//open existing file
File emailFile= new File( getFilesDir(), "sampleFile.txt" ); 
//allow read for others
emailFile.setReadable( true, false ); 
Uri emailFileUri = Uri.fromFile( emailFile );
Intent intentToEmailFile = new Intent( Intent.ACTION_SEND );
intentToEmailFile.setType( "message/rfc822" );
intentToEmailFile.putExtra(android.content.Intent.EXTRA_STREAM, emailFileUri );

Intent chooserIntent = Intent.createChooser( intentToEmailFile, "Send email" );
startActivity( chooserIntent );

That will attach an internal file correctly to the email client of your choice.

kiranpradeep
  • 10,859
  • 4
  • 50
  • 82
  • Ok. But not a solution for the problem. Why can the app read from its content provider but an exteral activity not? – greenapps May 27 '15 at 17:59
  • @greenapps I was assuming the problem was emailing an internal file. If it is about content providers, edit the question title to reflect that. – kiranpradeep May 27 '15 at 18:01
  • It would be nice if you could solve the content provider approach. And i'm not the OP! – greenapps May 27 '15 at 18:05
  • @greenapps sorry. I wrongly assumed you were op. About, content provider, let op decide which problem is to be solved. – kiranpradeep May 27 '15 at 18:07
  • I tried OP's code and it works if the app uses its content provider with the code OP posted. But it does not work for the SEND intent for external app. I cannot get it to work either. So independent from OP satisfied or not `It would be nice if you could solve the content provider approach.`. – greenapps May 27 '15 at 18:15
  • Also with setReadable(1) and with your code it share nothing, having error when attach file on drive,gmail,mail. Are you sure that'is ok ? This code is not working for me. – Alessio Di Marco May 28 '15 at 12:51
  • openFile(Uri uri, String mode) ..I noticed that i not use directly this method. It's correct? I'm very confused – Alessio Di Marco May 28 '15 at 12:57
  • @AlessioDiMarco have updated the answer. I tested and this is working. But, I couldn't understand why you referred to `setReadable(1)` and `openFile` which I haven't used in my answer. It is important to run the code exactly as is, since `Uri` has to be created from the same `File` object on which `setReadable` is set. – kiranpradeep May 29 '15 at 13:03
  • Granting raw access may or may not work, depending on the target. At at least one point in its evolution, the gmail client has refused to even attempt to access raw files that do not appear to be on the external storage - it just assumed it could not, without bothering to check if permission had actually been granted by the owner. – Chris Stratton May 29 '15 at 14:39
  • @ChrisStratton I assume that is before API 9 in which `setReadable(boolean readable, boolean ownerOnly)` was introduced. – kiranpradeep May 29 '15 at 14:49
  • No, I do not think it was related to that - it seemed to be an expected usage thing, not a technical thing. In actuality it has always been possible to set read permissions. – Chris Stratton May 29 '15 at 14:52
  • @ChrisStratton Why is it considered an expected usage ? If Android itself grants permission for world to read, who is deciding if it is non readable. And before API 9 which method in `File` class allowed the world to read ? – kiranpradeep May 29 '15 at 14:56
  • "Expected Usage" in the case of that issue is in the mind of the developer of the *target* application receiving the Intent. As for ending up with a world readable file, Context.openFileOutput(file, mode) goes back to API 1, and it has always been possible to chmod files by other means, too. – Chris Stratton May 29 '15 at 14:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79144/discussion-between-kiran-and-chris-stratton). – kiranpradeep May 29 '15 at 15:00