Android

[Android]Intent setType์‹œ ์‚ฌ์šฉ๋˜๋Š” mime type ์–ป๊ธฐ

devSunny99 2022. 11. 9. 14:58

๐Ÿ“Œ๊ฐœ์š”

์ฃผ๋กœ ํŒŒ์ผ์„ Intent๋ฅผ ํ†ตํ•˜์—ฌ ์ „๋‹ฌํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” mimeType ์–ป๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค์ฃผ๊ณ ์ž ํ•œ๋‹ค.

 

๐Ÿ“Œ๋ฐฉ๋ฒ•-1

โœ” MimeTypeMapํด๋ž˜์Šค ์‚ฌ์šฉํ•˜๊ธฐ

MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeType = mimeTypeMap.getMimeTypeFromExtension("pdf");
  • mimeType์€  "application/pdf" ์ผ ๊ฒƒ์ž„

 

โœ” ์ด๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค!

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
String filePath="ํŒŒ์ผ๊ฒฝ๋กœ๋ผ๊ณ  ๊ฐ€์ •";
intent.setType(mimeType);

 

๐Ÿ“Œ๋ฐฉ๋ฒ•-2

โœ” ๋‚ด๊ฐ€ ์ง์ ‘ ํ•˜๋‚˜ํ•˜๋‚˜ ํ•˜๋“œ์ฝ”๋”ฉํ•˜์—ฌ ํ•จ์ˆ˜๋กœ ๋นผ๊ธฐ

private String getFileMimeType(String filePath){
		String strMimeType = null;
		if(filePath ==null)
			return null;
		
		String fileName = new File(filePath).getName();
		if (fileName.endsWith(".doc")){
			strMimeType = "application/vnd.ms-word";
		}
		else if(fileName.endsWith(".docx")){
			strMimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
		}
		else if(fileName.endsWith(".xls")){
			strMimeType = "application/vnd.ms-excel";
		}
		else if(fileName.endsWith(".xlsx")){
			strMimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
		}
		else if(fileName.endsWith(".pptx")){
			strMimeType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
		}
		else if(fileName.endsWith(".ppt")){
			strMimeType = "application/vnd.ms-powerpoint";
		}
		else if(fileName.endsWith(".pdf")){
			strMimeType = "application/pdf";
		}
		else if(fileName.endsWith(".hwp")){
			strMimeType = "application/hwp";
		}
		else if(fileName.endsWith(".txt")){
			strMimeType = "text/plain";
		}
		else if(fileName.endsWith(".asc")){
			strMimeType = "text/plain";
		}
		else if(fileName.endsWith(".csv")){
			strMimeType = "text/comma-separated-values";
		}
		else if(fileName.endsWith(".rtf")){
			strMimeType = "text/rtf";
		}
		else if(fileName.endsWith(".pps")){
			strMimeType = "application/vnd.ms-powerpoint";
		}
		else if(fileName.endsWith(".ppsx")){
			strMimeType = "application/vnd.openxmlformats-officedocument.presentationml.slideshow";
		}
		
		return strMimeType;
	}
  • ์ฝ”๋“œ์— ์žˆ๋Š” ํ™•์žฅ์ž ๋ง๊ณ  ํ•„์š”ํ•œ ํ™•์žฅ์ž๋Š” Mime Type์„ ์ฐพ์•„์„œ ์ถ”๊ฐ€ํ•ด์ฃผ์ž

 

โœ” ์ด๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค!

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
String filePath="ํŒŒ์ผ๊ฒฝ๋กœ๋ผ๊ณ  ๊ฐ€์ •";
intent.setType(getFileMimeType(filePath));