簡體   English   中英

Android 打開畫廊在我的代碼中不起作用

[英]Android open gallery is not working in my code

我正在嘗試使用此代碼打開圖庫。 我的應用程序在圖庫中創建了一個目錄。 我可以在圖庫中看到我的目錄。 我正在嘗試通過以下代碼從我的應用程序中打開它:-

        File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File f = new File(sdDir, "Photo Location Note");
        Intent i = new Intent();
        i.setAction(Intent.ACTION_VIEW);
        i.setDataAndType(Uri.withAppendedPath(Uri.fromFile(f), "Photo Location Note"), "image/*");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);

它打開圖庫並顯示一條吐司消息“無法找到項目”。 它不會在圖庫中打開我的目錄“照片位置注釋”。

如何在圖庫中打開我的目錄“照片位置注釋”?

它適用於 Android 11 及更早版本:

    public void openPhoto(Context context, String directory){

    File file = new File(directory);
    if(!file.exists()){
        return;
    }

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ?
                    FileProvider.getUriForFile(context,context.getPackageName() + ".provider", file) : Uri.fromFile(file),"image/*");

    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent = Intent.createChooser(intent, "Open File");
    context.startActivity(intent);


}

這個片段很重要: intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM