Android - ฉันจะเปิดไฟล์ในแอปอื่นผ่าน Intent ได้อย่างไร

ฉันกำลังพยายามเปิดไฟล์โดยใช้แอปอื่น เช่น เปิด .jpg ด้วย Gallery, .pdf ด้วย Acrobat เป็นต้น

ปัญหาที่ฉันมีคือเมื่อฉันพยายามเปิดไฟล์ในแอป มันเปิดเฉพาะแอปที่เลือก แทนที่จะเปิดไฟล์ภายในแอป ฉันลองติดตาม ไฟล์ PDF แบบเปิดของ Android ผ่าน Intent แต่ฉันต้องขาดอะไรบางอย่างไป

public String get_mime_type(String url) {
    String ext = MimeTypeMap.getFileExtensionFromUrl(url);
    String mime = null;
    if (ext != null) {
        mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
    }
    return mime;
}

public void open_file(String filename) {
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS), filename);

    // Get URI and MIME type of file
    Uri uri = Uri.fromFile(file).normalizeScheme();
    String mime = get_mime_type(uri.toString());

    // Open file with user selected app
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(uri);
    intent.setType(mime);
    context.startActivity(Intent.createChooser(intent, "Open file with"));
}

เท่าที่ฉันสามารถบอกได้ มันจะส่งคืนประเภท URI และ MIME ที่ถูกต้อง:

URI: file:///storage/emulated/0/Download/Katamari-ringtone-985279.mp3
MIME: audio/mpeg

person Martin Himmel    schedule 24.07.2015    source แหล่งที่มา
comment
มีเพียงโน้ตเล็กๆ ที่เรียก setType() หลังจาก setData() ล้างข้อมูล ดังนั้นจึงควรใช้ setDataAndType()   -  person KaosXIV    schedule 03.06.2020


คำตอบ (2)


โพสต์การเปลี่ยนแปลงของฉันที่นี่เผื่อว่าสามารถช่วยคนอื่นได้ ฉันลงเอยด้วยการเปลี่ยนตำแหน่งการดาวน์โหลดเป็นโฟลเดอร์ภายในและเพิ่มผู้ให้บริการเนื้อหา

public void open_file(String filename) {
    File path = new File(getFilesDir(), "dl");
    File file = new File(path, filename);

    // Get URI and MIME type of file
    Uri uri = FileProvider.getUriForFile(this, App.PACKAGE_NAME + ".fileprovider", file);
    String mime = getContentResolver().getType(uri);

    // Open file with user selected app
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, mime);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);
}
person Martin Himmel    schedule 29.07.2015
comment
ฉันพยายามใช้มันในแอปของฉัน แต่มันมีพฤติกรรมแปลกๆ ฉันพยายาม Google แต่ไม่มีคำตอบ วิธีการนี้ใช้ได้กับแอปของบุคคลที่สามบางแอป และไม่สามารถใช้ได้กับแอปอื่นๆ บางแอป ฉันดาวน์โหลดไฟล์และส่ง Intent แต่แอปของบุคคลที่สามบางแอปไม่สามารถเปิดได้ หากฉันใช้เบราว์เซอร์เพื่อดาวน์โหลดไฟล์เดียวกันและเปิดไฟล์จากเบราว์เซอร์ (หรือตัวจัดการไฟล์) ด้วยแอปบุคคลที่สามเดียวกันก็จะทำงานได้ดี ฉันคิดว่ามีความลับที่ยุ่งยากในการที่เบราว์เซอร์และตัวจัดการไฟล์ส่งเจตนา คุณมีความคิด? - person bdevay; 29.12.2017
comment
ขอบคุณ ทำให้วันของฉันเพื่อน - person KhoaHA; 26.09.2019
comment
@devay ฉันมีปัญหาเดียวกัน คุณแก้ปัญหาได้หรือไม่? ถ้าเป็นเช่นนั้นคุณช่วยฉันได้ไหม? - person Roshan S; 13.06.2020

ฉันใช้โค้ดชิ้นนี้และทำงานได้อย่างสมบูรณ์แบบบน Android 6 และต่ำกว่าที่ไม่ได้ทดสอบกับเวอร์ชันที่สูงกว่า

public void openFile(final String fileName){
    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(new File(android.os.Environment.getExternalStorageDirectory()
            .getAbsolutePath()+ File.separator+"/Folder/"+fileName));
    intent.setDataAndType(uri, "audio/mpeg");
    startActivity(intent);
}
person Derrick    schedule 23.05.2019
comment
นี่เป็นเพียงการเปิดไฟล์เสียง/mpeg เท่านั้น ไม่จำเป็นต้องกล่าวถึงทั้งหมด - person Durgesh Kumar; 25.04.2020