Java 应用程序如何从库中选择照片
我正在测试一个android应用程序,该应用程序具有将手机图库中的照片设置为化身的功能。我的设备运行在API级别4.2.2上,我使用的是Appium 1.2.4.1。以及使用Java进行编码。我想知道如何才能访问特定的照片。第一个屏幕显示照片所在的所有根文件夹(例如100ANDRO),单击此按钮后,我可以访问照片。Inspector窗口不显示可选择的元素以导航到照片。以下是我从检查员那里看到的:Java 应用程序如何从库中选择照片,java,android,gallery,appium,Java,Android,Gallery,Appium,我正在测试一个android应用程序,该应用程序具有将手机图库中的照片设置为化身的功能。我的设备运行在API级别4.2.2上,我使用的是Appium 1.2.4.1。以及使用Java进行编码。我想知道如何才能访问特定的照片。第一个屏幕显示照片所在的所有根文件夹(例如100ANDRO),单击此按钮后,我可以访问照片。Inspector窗口不显示可选择的元素以导航到照片。以下是我从检查员那里看到的: 你知道我该如何访问照片并选择它吗?谢谢 您可以使用Intent访问图库图像- Intent
你知道我该如何访问照片并选择它吗?谢谢 您可以使用Intent访问图库图像-
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_PHOTO);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
并通过以下方式处理回调:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
}
}
}
/**
* helper to retrieve the path of an image URI
*/
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
// TODO perform some logging or show user feedback
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}
我认为这将帮助您:)您可以使用Intent访问图库图像-
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
并通过以下方式处理回调:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
}
}
}
/**
* helper to retrieve the path of an image URI
*/
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
// TODO perform some logging or show user feedback
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}
我想这会对你有所帮助:)简短地解释一下你在这里所做的事情会很有帮助。:-)正如我上面所说的,我正试图在Eclipse中使用appium(so selenium类)运行一个测试。因此,我认为我不能使用像Intent等不能回答问题的Android类。简短地解释一下您在这里所做的事情会很有帮助。:-)正如我上面所说的,我正试图在Eclipse中使用appium(so selenium类)运行一个测试。所以我想我不能使用像Intent这样的Android类,这不能回答这个问题。我正试图在Eclipse中使用appium(So selenium类)运行一个测试。所以我想我不能使用像Intent这样的Android类,这不能回答这个问题。我正试图在Eclipse中使用appium(So selenium类)运行一个测试。所以我认为我不能使用像Intent等不能回答问题的Android类。你可以通过inspector收集xpath来使用,但这很脆弱,因为它使你的测试依赖于应用程序的特定版本(尽管从外观上看,实际内容不在树中)使用inspector,您可以收集要使用的xpath,但这很脆弱,因为它使您的测试依赖于应用程序的特定版本(尽管从外观上看,实际内容不在树中)