สำหรับการทำงานโดยรวมก็จะคล้ายกับการแชร์ไฟล์ต่างๆ ที่เจ้าของบล็อกได้เคยพูดถึงไปแล้วในบทความก่อนหน้านี้ [Android Code] การใช้ Intent สำหรับแชร์ไฟล์ใดๆ [Send] แต่ในบทความดังกล่าวจะเป็นการ Send แต่คราวนี้จะเป็นการ View ซึ่งคำสั่งจะคล้ายกันเกือบทั้งหมดเลย (สบายเจ้าของบล็อกล่ะ)
สำหรับคราวนี้การกำหนด File Type จะกำหนดตามนามสกุลไฟล์ เพราะไม่เหมือนกับการแชร์ไฟล์กำหนดได้หลายๆ แบบ แต่การ View จะต้องกำหนดให้ตรงกับนามสกุลไฟล์เท่านั้น
โดยตัวอย่างนี้ก็จะใช้วิธีแบบเดิม คือเก็บไฟล์ไว้ที่โฟลเดอร์ assets พอจะนำมาเปิด จะให้ก๊อปไฟล์ไปไว้ใน External Storage หรือ SD ก่อน จากนั้นจึงกำหนดที่อยู่ไฟล์ให้กับ Intent เพื่อแสดงรายชื่อแอพที่จะเปิด
แต่ในการใช้งานจริงไฟล์ควรจะอยู่ใน External Storage หรือ SD เจ้าของบล็อกจึงได้ทำโค๊ดไว้ให้แล้วอีกชุด ในกรณีดังกล่าวจะใส่คอมเม้นไว้ให้ ส่วนการดึงจาก assets เป็นแค่ตัวอย่าง เพื่อที่ผู้ที่หลงเข้ามาอ่านจะได้ไม่ต้องมานั่งก๊อปไฟล์ลงเครื่องเอง
และเนื่องจากมีคำสั่งก๊อปไฟล์ลง External หรือ SD ในตัวอย่างนี้ด้วย ดังนั้นจึงต้องมีการประกาศ Permission เพื่อขอใช้งานดังกล่าวเช่นกัน โดยให้ประกาศไว้ใน AndroidManifest.xml (ทุกๆ Permission ประกาศที่นี่)
บทความนี้จะอธิบายน้อยหน่อย เพราะถือว่าเคยอธิบายไปแล้ว และในบทความนี้แตกต่างกันเล็กน้อย โดยโค๊ดทั้งหมดก็จะเป็นดังนี้
main.xml
Main.java
AndroidManifest.xml
สำหรับผู้ที่หลงเข้ามาอ่านคนใดที่ต้องการไฟล์ตัวอย่างสามารถดาวน์โหลดได้จาก Intent View File [Google Play]
บทความที่เกี่ยวข้อง
การใช้ Intent สำหรับแชร์ข้อความ String [Send]
การใช้ Intent สำหรับแชร์ข้อความสำหรับ Email [Send]
การใช้ Intent เพื่อเปิด URL [View]
การใช้ Intent เพื่อเปิดแผนที่ [View]
การใช้ Intent เพื่อเปิดไฟล์ใดๆ [View]
การเรียกเปิดแอพฯอื่นๆ ด้วย Intent
การใช้ Intent สำหรับแชร์ไฟล์ใดๆ [Send]
การเลือกไฟล์ภาพจาก Gallery ด้วย Intent [Result]
การใช้งานกล้องเพื่อถ่ายภาพแบบง่ายๆด้วย Intent [Result]
การใช้งานกล้องเพื่อบันทึกวีดีโอแบบง่ายๆด้วย Intent [Result]
การอ่าน QR Code และ Barcode ด้วย Intent [Result]
การรับข้อมูล Intent จากแอพฯอื่นๆ [Get Content]
การรับข้อมูล Intent จากแอพฯอื่นแล้วส่งข้อมูลกลับไป [Result Content]
Uri uri = Uri.fromFile(new File("file:///sdcard/tmp/image_logo.png"));
Intent intent = new Intent(Intent.ACTION_VIEW);
String mimeType = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(MimeTypeMap
.getFileExtensionFromUrl(file.getPath()));
intent.setDataAndType(uri, mimeType);
startActivity(Intent.createChooser(intent, "Open file with"));
โดยตัวอย่างนี้ก็จะใช้วิธีแบบเดิม คือเก็บไฟล์ไว้ที่โฟลเดอร์ assets พอจะนำมาเปิด จะให้ก๊อปไฟล์ไปไว้ใน External Storage หรือ SD ก่อน จากนั้นจึงกำหนดที่อยู่ไฟล์ให้กับ Intent เพื่อแสดงรายชื่อแอพที่จะเปิด
แต่ในการใช้งานจริงไฟล์ควรจะอยู่ใน External Storage หรือ SD เจ้าของบล็อกจึงได้ทำโค๊ดไว้ให้แล้วอีกชุด ในกรณีดังกล่าวจะใส่คอมเม้นไว้ให้ ส่วนการดึงจาก assets เป็นแค่ตัวอย่าง เพื่อที่ผู้ที่หลงเข้ามาอ่านจะได้ไม่ต้องมานั่งก๊อปไฟล์ลงเครื่องเอง
และเนื่องจากมีคำสั่งก๊อปไฟล์ลง External หรือ SD ในตัวอย่างนี้ด้วย ดังนั้นจึงต้องมีการประกาศ Permission เพื่อขอใช้งานดังกล่าวเช่นกัน โดยให้ประกาศไว้ใน AndroidManifest.xml (ทุกๆ Permission ประกาศที่นี่)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
บทความนี้จะอธิบายน้อยหน่อย เพราะถือว่าเคยอธิบายไปแล้ว และในบทความนี้แตกต่างกันเล็กน้อย โดยโค๊ดทั้งหมดก็จะเป็นดังนี้
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/buttonIntent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Open" />
</RelativeLayout>
Main.java
package app.akexorcist.intentviewfile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.MimeTypeMap;
import android.widget.Button;
public class Main extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**** Document from assets ****/
//final File file = assetToFile("worksheet/image_logo.png");
//final File file = assetToFile("worksheet/doc.docx");
//final File file = assetToFile("worksheet/IRF740.pdf");
//final File file = assetToFile("sound_effect.mp3");
final File file = assetToFile("my_video.mp4");
/**** Document from external storage (SD Card) ****/
//final File file = new File("file:///sdcard/tmp/image_logo.png");
//final File file = new File("file:///sdcard/tmp/doc.docx");
//final File file = new File("file:///sdcard/tmp/IRF740.pdf");
//final File file = new File("file:///sdcard/tmp/sound_effect.mp3");
//final File file = new File("file:///sdcard/tmp/my_video.mp4");
Button buttonIntent = (Button)findViewById(R.id.buttonIntent);
buttonIntent.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
String mimeType = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(MimeTypeMap
.getFileExtensionFromUrl(file.getPath()));
intent.setDataAndType(Uri.fromFile(file), mimeType);
startActivity(Intent.createChooser(intent, "Open file with"));
}
});
}
public File assetToFile(String filePath) {
new File(Environment.getExternalStorageDirectory().getAbsolutePath()
, "tmp").mkdir();
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
File file = new File(Environment.getExternalStorageDirectory()
, "tmp/" + fileName);
try {
InputStream is = getResources().getAssets().open(filePath);
OutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[is.available()];
is.read(buffer);
out.write(buffer, 0, buffer.length);
is.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.akexorcist.intentviewfile"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="app.akexorcist.intentviewfile.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
สำหรับผู้ที่หลงเข้ามาอ่านคนใดที่ต้องการไฟล์ตัวอย่างสามารถดาวน์โหลดได้จาก Intent View File [Google Play]
บทความที่เกี่ยวข้อง
การใช้ Intent สำหรับแชร์ข้อความ String [Send]
การใช้ Intent สำหรับแชร์ข้อความสำหรับ Email [Send]
การใช้ Intent เพื่อเปิด URL [View]
การใช้ Intent เพื่อเปิดแผนที่ [View]
การใช้ Intent เพื่อเปิดไฟล์ใดๆ [View]
การเรียกเปิดแอพฯอื่นๆ ด้วย Intent
การใช้ Intent สำหรับแชร์ไฟล์ใดๆ [Send]
การเลือกไฟล์ภาพจาก Gallery ด้วย Intent [Result]
การใช้งานกล้องเพื่อถ่ายภาพแบบง่ายๆด้วย Intent [Result]
การใช้งานกล้องเพื่อบันทึกวีดีโอแบบง่ายๆด้วย Intent [Result]
การอ่าน QR Code และ Barcode ด้วย Intent [Result]
การรับข้อมูล Intent จากแอพฯอื่นๆ [Get Content]
การรับข้อมูล Intent จากแอพฯอื่นแล้วส่งข้อมูลกลับไป [Result Content]