Thursday, December 5, 2013

Share file(s) using Intent

public void startSendToIntent(String packageName, String className, String mimetype, String urls) {

    // urls: "/storage/sdcard0/file1.mp4;/storage/sdcard0/file2.mp4;"
    // mimetype: "video/mp4"
    
    /* All url using ";" as the divider */
    String[] urlArray = urls.split(";");
    
    ArrayList uris = new ArrayList(urlArray.length);
    
    for (int i = 0; i < urlArray.length; i++) {
        uris.add(Uri.fromFile(new File(urlArray[i])));
    }
    
    Intent intent = new Intent();
    
    intent.setClassName(packageName, className);
    intent.setType(mimetype);
    
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    
    if (uris.size() > 1) {
        
        intent.setAction(Intent.ACTION_SEND_MULTIPLE);
        intent.putExtra(Intent.EXTRA_STREAM, uris);
        
    } else {
        
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM, uris.get(0));
        
    }
    
    Log.i(TAG, "startSendToIntent: packageName:"+packageName+" className:"+className+" mimetype:"+mimetype+" urls:"+urls+" uris:"+uris);
    
    mActivity.startActivity(intent);
}

No comments:

Post a Comment