gpt4 book ai didi

java - 如何使用一些外部文件管理器应用程序 Android 获取文件路径?

转载 作者:行者123 更新时间:2023-12-01 15:22:14 29 4
gpt4 key购买 nike

是否可以从 SD 卡获取文件,该文件是使用某些第三个应用程序(文件管理器)选取的?

我的意思是,我有一个带有按钮打开文件的 Activity ,当用户按下打开时,它会向他显示使用其他应用程序打开文件的建议,当他选择打开时,我会返回到我的带有该文件路径的 Activity ?

最佳答案

用这个

button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}
});

并在您的 Activity 中添加两个方法

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {

Uri selectedImageUri = data.getData();
String s1 = data.getDataString();
//String s1 = selectedImageUri.getPath();
Log.e("GetPath",s1);
//s1 = s1.replaceAll("file://","");
//Uri a = Uri.fromParts(s1,null,null);
Log.e("OK",""+selectedImageUri);
//Log.e("A",""+a);

selectedImagePath = getPath(selectedImageUri);
if(selectedImagePath==null && s1 != null)
{
selectedImagePath = s1.replaceAll("file://","");
}
// selectedImagePath = getPath(a);




Intent intent = new Intent(this, PhotoEditorActivity.class);
intent.putExtra("path", selectedImagePath);
startActivity(intent);
finish();


}
}
}


///////////////////////////////////
public String getPath(Uri uri) {

try{
String[] projection = { MediaStore.Images.Media.DATA };
Log.e("OK 1",""+projection);
Cursor cursor = managedQuery(uri, projection, null, null, null);
Log.e("OK 2",""+cursor);
if(cursor==null)
{
return null;

}
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
Log.e("OK 3",""+column_index);
cursor.moveToFirst();
Log.e("OK 4",""+cursor.getString(column_index));
return cursor.getString(column_index);
}
catch(Exception e)
{
Toast.makeText(PhotoActivity.this, "Image is too big in resolution please try again", 5).show();
return null;
}

}

并将此 int 添加为类成员

  private static final int SELECT_PICTURE = 1;

享受编码的乐趣..

关于java - 如何使用一些外部文件管理器应用程序 Android 获取文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10735409/

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