无法从 Nexus 5 Marshmallow 中的 Google 照片应用程序中选取照片和视频
Not able to pick photos and videos from Google photos app in nexus 5 marshmallow
我在使用 "google photos" 应用程序在 Nexus 5 中同时选取照片和视频时遇到问题。在其他应用程序中它工作正常,问题仅在于 google 照片。请帮助我,以便我可以看到完整的画廊(不仅是图片或视频)。
这是我按钮的点击:
@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);
}
我不确定为什么它不适合你,但如果照片没有保存在设备上,我在从 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);
}
您需要为图像和视频组合设置 setType()。
喜欢 :
intent.setType("image/* video/*");
此 setType() 适用于两种类型的媒体,您必须从图库中 select。
您可以使用它来选择所有类型的文件。
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
我在使用 "google photos" 应用程序在 Nexus 5 中同时选取照片和视频时遇到问题。在其他应用程序中它工作正常,问题仅在于 google 照片。请帮助我,以便我可以看到完整的画廊(不仅是图片或视频)。
这是我按钮的点击:
@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);
}
我不确定为什么它不适合你,但如果照片没有保存在设备上,我在从 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);
}
您需要为图像和视频组合设置 setType()。 喜欢 :
intent.setType("image/* video/*");
此 setType() 适用于两种类型的媒体,您必须从图库中 select。
您可以使用它来选择所有类型的文件。
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);