我发布了从画廊获取照片的意图,当我在我的画廊中使用nexus谷歌照片应用程序一切正常.
但如果图片不在手机上(在Google相片在线服务上),它会为我下载.选择图像后,我将图像发送到另一个活动进行裁剪,但在下载的情况下,发送到裁剪活动的图像为空,因为下载尚未完成.
如何知道下载完成后将图像发送到裁剪活动?
这是我的代码:
private void pickFromGallery() { Intent galleryIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(galleryIntent, RESULT_LOAD_IMG); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try { // When an Image is picked if (requestCode == RESULT_LOAD_IMG && resultCode == Activity.RESULT_OK && null != data) { // Get the Image from data Uri selectedImage = data.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; // Get the cursor Cursor cursor = getApplicationContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null); // Move to first row assert cursor != null; cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); imgDecodableString = cursor.getString(columnIndex); cursor.close(); startCrop(imgDecodableString); } } catch (Exception e) { Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) .show(); } }
任何帮助,将不胜感激.
我认为从谷歌照片下载所选图像时无法裁剪图像.您只能裁剪本地存储图像
但是,为了检查所选图像是否可下载或从本地存储中检查,您可以在onActivityResult()
方法中执行此操作.
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMG && resultCode == Activity.RESULT_OK && null != data) { Uri selectedImageUri = data.getData(); String tempPath = getPath(selectedImageUri, getActivity()); String url = data.getData().toString(); if (url.startsWith("content://com.google.android.apps.photos.content")){ try { InputStream is = getActivity().getContentResolver().openInputStream(selectedImageUri); if (is != null) { Bitmap pictureBitmap = BitmapFactory.decodeStream(is); //You can use this bitmap according to your purpose or Set bitmap to imageview } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else { startCrop(tempPath); } } }
这是getPath()
用于的方法onActivityResult()
.
public String getPath(Uri uri, Activity activity) { Cursor cursor = null; try { String[] projection = {MediaStore.MediaColumns.DATA}; cursor = activity.getContentResolver().query(uri, projection, null, null, null); if (cursor.moveToFirst()) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); return cursor.getString(column_index); } } catch (Exception e) { } finally { cursor.close(); } return ""; }
我希望它对你有所帮助.