![](/img/trans.png)
[英]Getting both Photos and Videos from the new Google Photos app on Android
[英]Not able to pick photos and videos from Google photos app in nexus 5 marshmallow
我在使用“ Google照片”应用程序在nexus 5中同时拾取照片和视频时遇到问题。 在其他应用程序中,它工作正常,问题仅在于Google照片。 请帮助我,以便我可以看到完整的画廊(不仅图片或视频)。
这是我按钮的onclick:
@Click(R.id.ivGallery)
void addMedia() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("*/*");
intent.setType("image/jpeg");
intent.setType("video/mp4");
startActivityForResult(intent, GALLERY_REQUEST_CODE);
}
您需要将setType()用于图像和视频的组合。 喜欢 :
intent.setType("image/* video/*");
该setType()用于必须从图库中选择的两种类型的媒体。
您可以使用它来选择所有类型的文件。
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
我不确定为什么它不适合您,但如果照片未保存在设备上,则从google-photos获取照片时会遇到类似的问题。 我所做的基本上是检查路径是否为空(意味着图像在设备上不存在),如果是,那么我只创建一个新文件并从流中读取一个缓冲区并将其保存到新文件中。
public static String getImagePathForImageDownloadedFromGooglePhotos(Context context, Uri uri)
{
ParcelFileDescriptor parcelFileDescriptor = null;
try {
parcelFileDescriptor = context.getContentResolver()
.openFileDescriptor(uri, "r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (parcelFileDescriptor == null)
return null;
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
InputStream inputStream = new FileInputStream(fileDescriptor);
try {
//noinspection SpellCheckingInspection
File file = new File(context.getCacheDir(), new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()));
String path = file.getAbsolutePath();
try {
try (OutputStream output = new FileOutputStream(file)) {
byte[] buffer = new byte[4 * 1024];
int read;
while ((read = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
return path;
}
} catch (Exception e) {
e.printStackTrace();
}
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
尝试这个。
@Click(R.id.ivGallery)
void addMedia() {
// ACTION_PICK : is showing many option
// ACTION_GET_CONTENT : only gallery
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// other setType calls aren't required.
intent.setType("*/*");
startActivityForResult(intent, GALLERY_REQUEST_CODE);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.