如何在 Android 中通过 Intent 打开特定文件夹?
在本文中,我们将从我们的应用程序中打开一个特定的文件夹。此功能在许多情况下很有用。大多数情况下,当我们想要上传任何文件时,应用程序只会打开文件管理器。但是我们可以实现此功能以有效使用,例如当任何用户想要上传任何文件时,用户只需选择文件夹名称,如 WhatsApp、下载等。然后用户将仅被定向到该文件夹。让我们看看这个功能的实现。
Reference Articles: Intent in Android
分步实施
第 1 步:创建一个新项目
要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。
步骤 2:使用 activity_main.xml 文件
导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。
XML
Java
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button movie, download, pic, music, whatsapp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
movie = findViewById(R.id.movies);
download = findViewById(R.id.download);
whatsapp = findViewById(R.id.whtsapp);
pic = findViewById(R.id.pic);
music = findViewById(R.id.music);
movie.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = Environment.getExternalStorageDirectory() + "/" + "Movies" + "/";
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
}
});
whatsapp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = Environment.getExternalStorageDirectory() + "/" + "WhatsApp" + "/";
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
}
});
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = Environment.getExternalStorageDirectory() + "/" + "Downloads" + "/";
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
}
});
music.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = Environment.getExternalStorageDirectory() + "/" + "Music" + "/";
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
}
});
pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = Environment.getExternalStorageDirectory() + "/" + "Pictures" + "/";
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
}
});
}
}
第 3 步:使用MainActivity。 Java文件
转到主活动。 Java文件,参考如下代码。下面是MainActivity的代码。 Java文件。代码中添加了注释以更详细地理解代码。
Java
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button movie, download, pic, music, whatsapp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
movie = findViewById(R.id.movies);
download = findViewById(R.id.download);
whatsapp = findViewById(R.id.whtsapp);
pic = findViewById(R.id.pic);
music = findViewById(R.id.music);
movie.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = Environment.getExternalStorageDirectory() + "/" + "Movies" + "/";
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
}
});
whatsapp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = Environment.getExternalStorageDirectory() + "/" + "WhatsApp" + "/";
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
}
});
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = Environment.getExternalStorageDirectory() + "/" + "Downloads" + "/";
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
}
});
music.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = Environment.getExternalStorageDirectory() + "/" + "Music" + "/";
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
}
});
pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = Environment.getExternalStorageDirectory() + "/" + "Pictures" + "/";
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
}
});
}
}
输出: