2017-09-22 46 views
5

是否可以從sdk v24開始在Android圖庫中打開相冊?Android:使用目標sdk打開圖庫相冊24

我有一個應用程序操縱圖像並將其保存到特定專輯名稱下的外部存儲。我想從應用程序有可能直接在Android圖庫中打開該相冊。這有可能實現嗎?

我可以輕鬆地打開一個文件,而不是整個專輯:

AndroidManifest.xml

... 
<provider 
    android:name="android.support.v4.content.FileProvider" 
    android:authorities="${applicationId}.provider" 
    android:exported="false" 
    android:grantUriPermissions="true"> 
    <meta-data 
     android:name="android.support.FILE_PROVIDER_PATHS" 
     android:resource="@xml/provider_paths"/> 
</provider> 
... 

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <external-path name="external_files" path="."/> 
</paths> 

我打開特定文件的處理:

Intent i = new Intent(); 
i.setAction(Intent.ACTION_VIEW); 
File myFile = new File(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), MY_ALBUM_NAME), MY_FILE_NAME); 
Uri myUri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", myFile); 
i.setDataAndType(myUri, "image/*"); 
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
startActivity(i); 

打開一個特定的文件工作得很好。但是,我無法打開整張專輯。那可能嗎?

回答

1

您可以掃描您相冊中的圖像,並在您創建的活動中顯示它們,而不是在您的相冊上打開圖庫。

public class MainActivity extends AppCompatActivity implements MediaScannerConnection.MediaScannerConnectionClient { 
public String[] allFiles; 
private String SCAN_PATH ; 
private static final String FILE_TYPE="image/*"; 

private MediaScannerConnection conn; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_main); 

    File folder = Environment 
      .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/YourAlbumName"); 
    allFiles = folder.list(); 
    for(int i=0;i<allFiles.length;i++) 
    { 
     Log.d("all file path"+i, allFiles[i]+allFiles.length); 
    } 


    SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/Photo/"+allFiles[0]; 
    System.out.println(" SCAN_PATH " +SCAN_PATH); 

    Log.d("SCAN PATH", "Scan Path " + SCAN_PATH); 
    Button scanBtn = (Button)findViewById(R.id.button); 
    scanBtn.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      startScan(); 
     }}); 
} 
private void startScan() 
{ 
    Log.d("Connected","success"+conn); 
    if(conn!=null) 
    { 
     conn.disconnect(); 
    } 
    conn = new MediaScannerConnection(this,this); 
    conn.connect(); 
} 
@Override 
public void onMediaScannerConnected() { 
    Log.d("onMediaScannerConnected","success"+conn); 
    conn.scanFile(SCAN_PATH, FILE_TYPE); 
} 
@Override 
public void onScanCompleted(String path, Uri uri) { 
    try { 
     Log.d("onScanCompleted",uri + "success"+conn); 
     System.out.println("URI " + uri); 
     if (uri != null) 
     { 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setData(uri); 
      startActivity(intent); 
     } 
    } finally 
    { 
     conn.disconnect(); 
     conn = null; 
    } 
} 

}

+0

嘿,感謝您的答覆。是的,那是我無法在畫廊中打開專輯的解決方案。但我正在尋找一個明確的答案 - 我可以在默認圖庫應用程序中打開相冊嗎? –