simple Share Intent with saved image

edited May 2016 in Android Mode

I've been reading the android documentation about this feature, here, and I was trying to think about how to share a saved frame with an Intent instance. Any step or tip is welcome in order to read more about it. I guess this could be a nice question, not just for me but for anyone.

This is the basic part. I'm not sure if the saved frame should be converted to uri, or there is another way to do it.

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

Answers

  • edited November 2013

    ok, I've got something more. The Intent Chooser works, although a grey image is attached, as it has no information. I'm not sure if I'm doing well with the URI.

    image alt text

    import android.content.Intent;
    import android.content.Context;
    import android.net.Uri;
    
    PImage tmp, img;
    Uri uri;
    
    void setup() {
      tmp = loadImage("9ume.png");
      size(displayWidth, displayHeight);
    
      orientation(LANDSCAPE);
      img = new PImage(width, height);
      img.copy(tmp, 0, 0, tmp.width, tmp.height, 0, 0, width, height);
      background(0);
    
      uri = Uri.fromFile(getFileStreamPath("9ume.png"));
    
      image(img, 0, 0);
    }
    
    void draw() {
    }
    
    
    void mousePressed() {
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/png");
    startActivity(Intent.createChooser(shareIntent, "Where?"));
    }
    
  • My best guess is that the file 9ume.png is stored in the data folder, while the function getFileStreamPath() is searching the app's private directory (the sketch folder), although I may be wrong. You can (probably) check for this by printing out the URI's actual file location.

  • I'll check it out calsign, thanks!

  • edited November 2013

    println(uri.getPath()) prints "/data/data/processing.test.imagen/files/9ume.png"

    It seems right, but doesn't work. I've tried with external files in sdcard and works!

    By the way, I don't know why when saving a file to Pictures directory it doesn't show up in the Gallery, but I can see it within the File Manager.

    i.e

    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + 
                              File.separator + "AppFolder" + File.separator 
                                +year()+month()+day()+"_"+hour()+minute()+second()+".png");
    file.getParentFile().mkdirs();
    

    EDIT1:

    After spending hours searching, I've tried adding startBroadcast(intent) and console throws "processing.app.SketchException: cannot find symbol" at that line. (I've already imported android.content.Context and android.content.Intent)

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));

    EDIT2:

    EDIT1 worked.

  • The data folder appears twice in the file's path. Is this intentional?

    You might try getting the full path of the file and (if your device is rooted), you can then navigate to that file. Of course, this isn't an option if your device isn't rooted...

    EDIT: It wasn't clear... but were you able to get it to work? I can't tell from your string of edits...

  • edited November 2013

    /data/data is strange but it's an Android convention. To access there the phone must have been rooted, you're right.

    In "EDIT2" I commented that what I posted in "EDIT1" worked 8-X

    I'm not sure why I was having that error.

    Here's the code working,

    import android.os.Environment;
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import java.io.File;
    
        //...
        e = new AnimatedGifEncoder(); //a gif encoder for java micro edition
    
        file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + 
          File.separator + "AppFolder" + File.separator +
          year()+month()+day()+"_"+hour()+minute()+second()+".gif");
        file.getParentFile().mkdirs(); //if the folder doesn't exists it's created
    
        e.start(file.getAbsolutePath()); //path to the encoder
        //...adding frames to the encoder
        e.addFrame(pg.get().pixels, width, height); 
        //...
        e.finish();
    
        Uri uri = Uri.fromFile(file); //get uri from file
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); //refresh gallery to include the new file
        share(uri); //share
    
        //....
    }
    
    void share(Uri uri) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        shareIntent.setType("image/gif");
        startActivity(Intent.createChooser(shareIntent, "->"));
      }
    

    The Animated Gif Encoder is a ported version of the original one.

Sign In or Register to comment.