gpt4 book ai didi

Android:使用目标 sdk 24 打开图库相册

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:08 26 4
gpt4 key购买 nike

是否可以从 sdk v24 开始在 Android Gallery 中打开相册?

我有一个应用程序可以处理图像并将其保存到特定相册名称下的外部存储中。我希望有可能从应用程序直接在 Android Gallery 中打开该相册。有可能实现吗?

我可以轻松打开单个文件,但不能打开整个相册:

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

打开特定文件工作正常。但是,我无法打开整个相册。这可能吗?

最佳答案

您可以扫描相册中的图像并将它们显示在您创建的 Activity 中,而不用打开相册中的图库。

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

关于Android:使用目标 sdk 24 打开图库相册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46368906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com