【问题标题】:how to display pictures from phone in cards in android studio (like whatsapp does)?如何在android studio的卡片中显示手机中的图片(如whatsapp)?
【发布时间】:2020-10-13 06:49:13
【问题描述】:
我想显示用户在手机中拥有的不同图片,例如用户在卡片库中拥有的所有相册/文件夹的卡片中的应用程序(例如:应用程序图片、下载、所有照片等)。 .)。当用户点击卡片时,它也会在卡片中显示该文件夹中的所有图像。谁能告诉我如何访问这些文件夹,因为这是主要问题?
【问题讨论】:
标签:
android
android-studio
whatsapp
【解决方案1】:
你可以试试这样的:
private static final int PICKFILE_RESULT_CODE = 1;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(
intent,
"Select Image from here..."),
PICKFILE_RESULT_CODE);
使用它,您可以让用户导航到画廊并选择图像。
【解决方案2】:
WhatsApp 的作用是启动一个Intent,调用安装用户手机的默认图库应用程序。如另一个答案所述,您需要启动一个画廊意图,在该意图中您只能向用户显示图像。此 Intent 返回一个代码,该代码告诉启动该 Intent 的应用程序文件选择是否成功,根据该结果,您可以修改您的代码。
这是一个例子:-
Intent openFileManager = new Intent(Intent.ACTION_GET_CONTENT); //For Chosing to Open the file manager
openFileManager.setType("image/*"); //for telling the file manager that only files of type image should be returned
startActivityForResult(openFileManager, 12); //starting the intent, here 12 is the code that tells whether it was successful or not
用于处理用户选择的结果:-
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
if(requestCode == 12 && resultCode == RESULT_OK) //requestCode tells which intent to look for and resultCode tells whether the result is okay or not
{
//Your Code goes here
//The file which has been returned is in the Intent, you can get that file from Intent "data"
}
else
{
super.onActivityResult(requestCode, resultCode, data);
}
}