`
yuhushuan
  • 浏览: 8248 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android 分享

阅读更多
第一种方法使用地址
String url = "file:///" + "sdcard/download/filename";//filename带扩展名
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
shareIntent.putExtra(Intent.EXTRA_TEXT, body);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
shareIntent.setType(getMIMEType(filename));
context.startActivity(shareIntent);

         /**
	 * 得到打开文件的类型
	 * 
	 * @param fileExtensionName
	 * @return
	 */
	public static String getMIMEType(String fileExtensionName) {
		String type = "";
		String end = fileExtensionName.substring(fileExtensionName.lastIndexOf(".") + 1).toLowerCase();
		if (end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf") || end.equals("ogg")
				|| end.equals("wav")) {
			type = "audio";
		} else if (end.equals("3gp") || end.equals("mp4")) {
			type = "video";
		} else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg")
				|| end.equals("bmp")) {
			type = "image";
		} else if (end.equals("apk")) {
			/* android.permission.INSTALL_PACKGES */
			type = "application/vnd.android.package-archive";
		} else {
			type = "*";
		}
		if (end.equals("apk")) {
		} else {
			type += "/*";
		}
		return type;
	}

文件格式为apk文件时,会使用邮件发送,不是用蓝牙。
第二种使用流方式
private void startSendIntent() { 
        Bitmap bitmap = Bitmap.createBitmap(editableImageView.getWidth(), editableImageView.getHeight(), Bitmap.Config.RGB_565); 
        editableImageView.draw(new Canvas(bitmap)); 
        File png = getFileStreamPath(getString(R.string.file_name)); 
        FileOutputStream out = null; 
        try { 
            out = openFileOutput(getString(R.string.file_name), MODE_WORLD_READABLE); 
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 
            out.flush(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            try { 
                if (out != null) out.close(); 
            } 
            catch (IOException ignore) {} 
        } 
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(png)); 
        emailIntent.setType("image/png"); 
        startActivity(Intent.createChooser(emailIntent, getString(R.string.send_intent_name))); 
} 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics