Sharing File (Content) without saving it before The Next CEO of Stack OverflowLimiting the save content of file to certain sizeGuessing a file type based on its contentSaving and restoring RadixTree objectReading a text file API 15+String parsing and saving ipaddress to file in JavaSaving encrypted filesGuessing file type from contentSaving temporary file for plotting audio waveformSaving the contents of a TextArea to a fileOptimize the file content comparison

Why, when going from special to general relativity, do we just replace partial derivatives with covariant derivatives?

Axiom Schema vs Axiom

How do I align (1) and (2)?

Is it possible to replace duplicates of a character with one character using tr

Does Germany produce more waste than the US?

Is there a way to save my career from absolute disaster?

Newlines in BSD sed vs gsed

When you upcast Blindness/Deafness, do all targets suffer the same effect?

Rotate a column

How to place nodes around a circle from some initial angle?

Dominated convergence theorem - what sequence?

Find non-case sensitive string in a mixed list of elements?

Why did CATV standarize in 75 ohms and everyone else in 50?

Bartok - Syncopation (1): Meaning of notes in between Grand Staff

A Man With a Stainless Steel Endoskeleton (like The Terminator) Fighting Cloaked Aliens Only He Can See

Would a completely good Muggle be able to use a wand?

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

Where do students learn to solve polynomial equations these days?

What steps are necessary to read a Modern SSD in Medieval Europe?

Is French Guiana a (hard) EU border?

How many extra stops do monopods offer for tele photographs?

Why doesn't UK go for the same deal Japan has with EU to resolve Brexit?

I want to delete every two lines after 3rd lines in file contain very large number of lines :

How did people program for Consoles with multiple CPUs?



Sharing File (Content) without saving it before



The Next CEO of Stack OverflowLimiting the save content of file to certain sizeGuessing a file type based on its contentSaving and restoring RadixTree objectReading a text file API 15+String parsing and saving ipaddress to file in JavaSaving encrypted filesGuessing file type from contentSaving temporary file for plotting audio waveformSaving the contents of a TextArea to a fileOptimize the file content comparison










2












$begingroup$


I have a listview of PrivateFile objects which contain the fileId (int), fileName (String) and the fileContent (byte []). In order to share the file, I currently save the file first, open it again, and then I share it. In my opinion this is really semi-professional. Is there a way wherein I can only share the file (content)/byte array without saving it before? Or can I improve my code in general?



privateDocs.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() 
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id)
try
PrivateFile selFile = (PrivateFile) arg0.getItemAtPosition(pos);

File folder = new File(Environment.getExternalStorageDirectory() + "/myFolder");
boolean success = true;
if (!folder.exists())
success = folder.mkdir();

if (success)
//safe file in order to open it later
File file = new File(folder.getAbsolutePath() + "/" + selFile.getFileName());
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
fos.write(selFile.getFileContent());
fos.close();


Intent intentShareFile = new Intent(Intent.ACTION_SEND);

if(file.exists())
String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString()));
intentShareFile.setType(newMimeType);
intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file.getAbsolutePath()));
intentShareFile.putExtra(Intent.EXTRA_SUBJECT,
"Sharing File...");
intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File...");

startActivity(Intent.createChooser(intentShareFile, "Share File"));


else
throw new Exception("could not open folder");

catch (Exception ex)
System.out.println("Err:" + ex.getMessage());

return true;

);









share|improve this question











$endgroup$
















    2












    $begingroup$


    I have a listview of PrivateFile objects which contain the fileId (int), fileName (String) and the fileContent (byte []). In order to share the file, I currently save the file first, open it again, and then I share it. In my opinion this is really semi-professional. Is there a way wherein I can only share the file (content)/byte array without saving it before? Or can I improve my code in general?



    privateDocs.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() 
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id)
    try
    PrivateFile selFile = (PrivateFile) arg0.getItemAtPosition(pos);

    File folder = new File(Environment.getExternalStorageDirectory() + "/myFolder");
    boolean success = true;
    if (!folder.exists())
    success = folder.mkdir();

    if (success)
    //safe file in order to open it later
    File file = new File(folder.getAbsolutePath() + "/" + selFile.getFileName());
    FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
    fos.write(selFile.getFileContent());
    fos.close();


    Intent intentShareFile = new Intent(Intent.ACTION_SEND);

    if(file.exists())
    String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString()));
    intentShareFile.setType(newMimeType);
    intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file.getAbsolutePath()));
    intentShareFile.putExtra(Intent.EXTRA_SUBJECT,
    "Sharing File...");
    intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File...");

    startActivity(Intent.createChooser(intentShareFile, "Share File"));


    else
    throw new Exception("could not open folder");

    catch (Exception ex)
    System.out.println("Err:" + ex.getMessage());

    return true;

    );









    share|improve this question











    $endgroup$














      2












      2








      2





      $begingroup$


      I have a listview of PrivateFile objects which contain the fileId (int), fileName (String) and the fileContent (byte []). In order to share the file, I currently save the file first, open it again, and then I share it. In my opinion this is really semi-professional. Is there a way wherein I can only share the file (content)/byte array without saving it before? Or can I improve my code in general?



      privateDocs.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() 
      @Override
      public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id)
      try
      PrivateFile selFile = (PrivateFile) arg0.getItemAtPosition(pos);

      File folder = new File(Environment.getExternalStorageDirectory() + "/myFolder");
      boolean success = true;
      if (!folder.exists())
      success = folder.mkdir();

      if (success)
      //safe file in order to open it later
      File file = new File(folder.getAbsolutePath() + "/" + selFile.getFileName());
      FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
      fos.write(selFile.getFileContent());
      fos.close();


      Intent intentShareFile = new Intent(Intent.ACTION_SEND);

      if(file.exists())
      String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString()));
      intentShareFile.setType(newMimeType);
      intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file.getAbsolutePath()));
      intentShareFile.putExtra(Intent.EXTRA_SUBJECT,
      "Sharing File...");
      intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File...");

      startActivity(Intent.createChooser(intentShareFile, "Share File"));


      else
      throw new Exception("could not open folder");

      catch (Exception ex)
      System.out.println("Err:" + ex.getMessage());

      return true;

      );









      share|improve this question











      $endgroup$




      I have a listview of PrivateFile objects which contain the fileId (int), fileName (String) and the fileContent (byte []). In order to share the file, I currently save the file first, open it again, and then I share it. In my opinion this is really semi-professional. Is there a way wherein I can only share the file (content)/byte array without saving it before? Or can I improve my code in general?



      privateDocs.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() 
      @Override
      public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id)
      try
      PrivateFile selFile = (PrivateFile) arg0.getItemAtPosition(pos);

      File folder = new File(Environment.getExternalStorageDirectory() + "/myFolder");
      boolean success = true;
      if (!folder.exists())
      success = folder.mkdir();

      if (success)
      //safe file in order to open it later
      File file = new File(folder.getAbsolutePath() + "/" + selFile.getFileName());
      FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
      fos.write(selFile.getFileContent());
      fos.close();


      Intent intentShareFile = new Intent(Intent.ACTION_SEND);

      if(file.exists())
      String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString()));
      intentShareFile.setType(newMimeType);
      intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file.getAbsolutePath()));
      intentShareFile.putExtra(Intent.EXTRA_SUBJECT,
      "Sharing File...");
      intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File...");

      startActivity(Intent.createChooser(intentShareFile, "Share File"));


      else
      throw new Exception("could not open folder");

      catch (Exception ex)
      System.out.println("Err:" + ex.getMessage());

      return true;

      );






      java performance android file






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 7 mins ago









      Jamal

      30.5k11121227




      30.5k11121227










      asked Mar 11 at 8:17









      Lebron11Lebron11

      111




      111




















          0






          active

          oldest

          votes












          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215173%2fsharing-file-content-without-saving-it-before%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215173%2fsharing-file-content-without-saving-it-before%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          कुँवर स्रोत दिक्चालन सूची"कुँवर""राणा कुँवरके वंशावली"

          शेव्रोले वोल्ट अनुक्रम इतिहास इन्हे भी देखें चित्र दीर्घा संदर्भ दिक्चालन सूची

          चैत्य भूमि चित्र दीर्घा सन्दर्भ बाहरी कडियाँ दिक्चालन सूची"Chaitya Bhoomi""Chaitya Bhoomi: Statue of Equality in India""Dadar Chaitya Bhoomi: Statue of Equality in India""Ambedkar memorial: Centre okays transfer of Indu Mill land"चैत्यभमि