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));