Java Code Examples for android.content.Intent#getClipData()

The following examples show how to use android.content.Intent#getClipData() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public static GrantedUriPermissions createFromIntent(IActivityManager am, Intent intent,
        int sourceUid, String targetPackage, int targetUserId, String tag) {
    int grantFlags = intent.getFlags();
    if (!checkGrantFlags(grantFlags)) {
        return null;
    }

    GrantedUriPermissions perms = null;

    Uri data = intent.getData();
    if (data != null) {
        perms = grantUri(am, data, sourceUid, targetPackage, targetUserId, grantFlags, tag,
                perms);
    }

    ClipData clip = intent.getClipData();
    if (clip != null) {
        perms = grantClip(am, clip, sourceUid, targetPackage, targetUserId, grantFlags, tag,
                perms);
    }

    return perms;
}
 
Example 2
Source Project: belvedere   File: MediaSource.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Extract {@link Uri} from an {@link Intent} that comes back from a gallery
 * or the android document picker.
 *
 * <p>
 *      If the user selects multiple media files, it's necessary to check if
 *      {@link Intent#getClipData()} contains data. Support for selecting multiple
 *      files was introduced with Android Jelly Bean.
 *      {@link android.os.Build.VERSION_CODES#JELLY_BEAN}
 *      <br>
 *      A single selected item is available by calling {@link Intent#getData()}.
 *      <br>
 *      Pretty messed up :/
 * </p>
 *
 * @param intent The returned {@link Intent}, containing {@link Uri} to the selected
 *               media
 * @return A list of {link Uri}
 */
@SuppressLint("NewApi")
private List<Uri> extractUrisFromIntent(Intent intent){
    final List<Uri> images = new ArrayList<>();

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && intent.getClipData() != null) {
        final ClipData clipData = intent.getClipData();

        for (int i = 0, itemCount = clipData.getItemCount(); i < itemCount; i++) {
            final ClipData.Item itemAt = clipData.getItemAt(i);

            if (itemAt.getUri() != null) {
                images.add(itemAt.getUri());
            }
        }
    } else if(intent.getData() != null){
        images.add(intent.getData());
    }

    return images;
}
 
Example 3
Source Project: styT   File: Main2Activity.java    License: Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void onActivityResultAboveL(int requestCode, int resultCode, Intent intent) {
    if (requestCode != FILE_CHOOSER_RESULT_CODE || uploadMessageAboveL == null) return;
    Uri[] results = null;
    if (resultCode == AppCompatActivity.RESULT_OK) {
        if (intent != null) {
            String dataString = intent.getDataString();
            ClipData clipData = intent.getClipData();
            if (clipData != null) {
                results = new Uri[clipData.getItemCount()];
                for (int i = 0; i < clipData.getItemCount(); i++) {
                    ClipData.Item item = clipData.getItemAt(i);
                    results[i] = item.getUri();
                }
            }
            if (dataString != null)
                results = new Uri[]{Uri.parse(dataString)};
        }
    }
    uploadMessageAboveL.onReceiveValue(results);
    uploadMessageAboveL = null;
}
 
Example 4
Source Project: RxGallery   File: RxGalleryActivity.java    License: MIT License 6 votes vote down vote up
private ArrayList<Uri> handleGallery(Intent data) {
    ArrayList<Uri> uris = new ArrayList<>();
    if (data.getData() != null) { // Single select
        uris.add(data.getData());
    } else { // Multi select
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            ClipData clipData = data.getClipData();
            if (clipData != null) {
                for (int i = 0; i < clipData.getItemCount(); i++) {
                    uris.add(clipData.getItemAt(i).getUri());
                }
            }
        }
    }
    return uris;
}
 
Example 5
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_CODE_GET_FILES) {
        if (resultCode != Activity.RESULT_OK || data == null)
            return;

        if (data.getData() != null) {
            mViewModel.installPackagesFromContentProviderZip(data.getData());
            return;
        }

        if (data.getClipData() != null) {
            ClipData clipData = data.getClipData();
            List<Uri> apkUris = new ArrayList<>(clipData.getItemCount());

            for (int i = 0; i < clipData.getItemCount(); i++)
                apkUris.add(clipData.getItemAt(i).getUri());

            mViewModel.installPackagesFromContentProviderUris(apkUris);
        }
    }
}
 
Example 6
@Override
public void fromIntentSearch(Intent intent) {
    String keyWord = null;
    if (intent != null) {
        keyWord = intent.getStringExtra("searchKey");
        if (keyWord == null && intent.getClipData() != null && intent.getClipData().getItemCount() > 0) {
            ClipData.Item item = intent.getClipData().getItemAt(0);
            keyWord = StringUtils.valueOf(item.getText()).trim();

            keyWord = TextProcessor.formatKeyword(keyWord);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && keyWord == null && Intent.ACTION_PROCESS_TEXT.equals(intent.getAction())) {
            String type = intent.getType();
            if ("text/plain".equals(type)) {
                keyWord = TextProcessor.formatKeyword(intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT));
            }
        }
    }

    mView.searchBook(keyWord);
}
 
Example 7
public static List<RequestFile> onActivityResult(Context context, Intent data) {
    List<RequestFile> files = new ArrayList<>();
    RequestFile tempFile;
    Log.d(LOG_TAG, "onActivityResult " + data);
    if (data.getData() == null) {
        if (data.getClipData() != null) {
            for (int i = 0; i < data.getClipData().getItemCount(); i++) {
                tempFile = createFile(context, data.getClipData().getItemAt(i).getUri());
                if (tempFile != null) files.add(tempFile);
            }
        }
    } else {
        tempFile = createFile(context, data.getData());
        if (tempFile != null) files.add(tempFile);
    }
    return files;
}
 
Example 8
Source Project: androidnative.pri   File: ImagePicker.java    License: Apache License 2.0 6 votes vote down vote up
static private void importImage(Intent data) {
    Uri uri = data.getData();

    Log.d(TAG,"importImage: uri:" + uri);
    Log.d(TAG,"importImage: type: " + data.getType());

    if (data.getClipData() != null) {
        importImageFromClipData(data);
    } else if (uri != null ) {
        if (uri.getScheme().equals("file")) {
            importImageFromFileUri(uri);
        } else {
            importImageFromContentUri(uri);
        }
    }

}
 
Example 9
Source Project: quickimagepick   File: QiPick.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private static void handleResultFromDocuments(final int pRequestType, @NonNull final PickCallback pCallback, @Nullable final Intent pData) {

    final Uri pictureUri = pData == null ? null : pData.getData();

    if (pictureUri == null) {

        final ClipData clipData = API_18 ? pData != null ? pData.getClipData() : null : null;
        if (clipData != null) {

            final ArrayList<Uri> uris = new ArrayList<>();
            for (int i = 0; i < clipData.getItemCount(); i++) {

                final ClipData.Item item = clipData.getItemAt(i);

                final Uri uri = item.getUri();

                if (uri != null) {
                    uris.add(uri);
                }

            }

            if (uris.isEmpty()) {
                pCallback.onError(PickSource.DOCUMENTS, pRequestType, ERR_DOCS_NULL_RESULT);
            } else {
                pCallback.onMultipleImagesPicked(pRequestType, uris);
            }

        } else {
            pCallback.onError(PickSource.DOCUMENTS, pRequestType, ERR_DOCS_NULL_RESULT);
        }

    } else {
        pCallback.onImagePicked(PickSource.DOCUMENTS, pRequestType, pictureUri);
    }

}
 
Example 10
Source Project: cloudinary_android   File: MainActivity.java    License: MIT License 5 votes vote down vote up
private void uploadFromIntentUri(Intent data) {
    Uri uri = data.getData();
    if (uri != null) {
        handleUri(uri, data.getFlags());
    } else if (data.getClipData() != null) {
        ClipData clip = data.getClipData();
        for (int i = 0; i < clip.getItemCount(); i++) {
            handleUri(clip.getItemAt(i).getUri(), data.getFlags());
        }
    }
}
 
Example 11
Source Project: hash-checker   File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void create() {
    Intent intent = getIntent();
    String scheme = null;
    ClipData clipData = null;
    if (intent != null) {
        scheme = intent.getScheme();
        clipData = intent.getClipData();
    }
    Uri externalFileUri = null;
    if (clipData != null) {
        externalFileUri = clipData.getItemAt(0).getUri();
    }

    HashCalculatorFragment mainFragment = new HashCalculatorFragment();
    if (scheme != null && scheme.compareTo(ContentResolver.SCHEME_CONTENT) == 0) {
        mainFragment.setArguments(getConfiguredBundleWithDataUri(intent.getData()));
        SettingsHelper.setGenerateFromShareIntentMode(this, true);
    } else if (externalFileUri != null) {
        mainFragment.setArguments(getConfiguredBundleWithDataUri(externalFileUri));
        SettingsHelper.setGenerateFromShareIntentMode(this, true);
    } else {
        mainFragment.setArguments(getBundleForShortcutAction(intent.getAction()));
        SettingsHelper.setGenerateFromShareIntentMode(this, false);
    }

    showFragment(mainFragment);
}
 
Example 12
Source Project: zulip-android   File: ZulipActivity.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        // send file path to PhotoSendActivity
        Intent photoSendIntent = new Intent(this, PhotoSendActivity.class);
        photoSendIntent.putExtra(Intent.EXTRA_TEXT, mCurrentPhotoPath);
        startActivity(photoSendIntent);

        // activity transition animation
        ActivityTransitionAnim.transition(ZulipActivity.this);
    } else if (requestCode == REQUEST_PICK_FILE && resultCode == RESULT_OK) {
        List<Uri> fileUris = new ArrayList<>();
        if (data.getData() != null) {
            fileUris.add(data.getData());
        }
        // intent.getClipData was added in api 16
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            ClipData clipData = data.getClipData();
            if (clipData != null) {
                for (int i = 0; i < clipData.getItemCount(); i++) {
                    fileUris.add(clipData.getItemAt(i).getUri());
                }
            }
        }
        for (Uri file : fileUris) {
            handleSentFile(file);
        }
    }
}
 
Example 13
Source Project: mage-android   File: ProfileActivity.java    License: Apache License 2.0 5 votes vote down vote up
@TargetApi(16)
private List<Uri> getClipDataUris(Intent intent) {
	List<Uri> uris = new ArrayList<>();
	if (Build.VERSION.SDK_INT >= 16) {
		ClipData cd = intent.getClipData();
		if (cd != null) {
			for (int i = 0; i < cd.getItemCount(); i++) {
				uris.add(cd.getItemAt(i).getUri());
			}
		}
	}
	return uris;
}
 
Example 14
static Bundle getResultsFromIntent(Intent intent) {
    ClipData clipData = intent.getClipData();
    if (clipData == null) {
        return null;
    }
    ClipDescription clipDescription = clipData.getDescription();
    if (!clipDescription.hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)) {
        return null;
    }
    if (clipDescription.getLabel().equals(RESULTS_CLIP_LABEL)) {
        return clipData.getItemAt(0).getIntent().getExtras().getParcelable(EXTRA_RESULTS_DATA);
    }
    return null;
}
 
Example 15
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    firstTime = false;
    JSONArray jsonArray = new JSONArray();

    // Retrieve file, folders paths
    if (requestCode == FILE_CODE && resultCode == Activity.RESULT_OK) {
        if (data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false)) {
            // For JellyBean and above
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                ClipData clip = data.getClipData();

                if (clip != null) {
                    for (int i = 0; i < clip.getItemCount(); i++) {
                        jsonArray.put(clip.getItemAt(i).getUri().toString());
                    }
                }
            // For Ice Cream Sandwich
            } else {
                ArrayList<String> paths = data.getStringArrayListExtra
                            (FilePickerActivity.EXTRA_PATHS);

                if (paths != null) {
                    for (String path: paths) {
                        jsonArray.put(Uri.parse(path).toString());
                    }
                }
            }

        } else {
            jsonArray.put(data.getData().toString());
        }
    }

    // Send information
    Intent intent = new Intent();
    intent.putExtra("information", jsonArray.toString());
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    setResult(Activity.RESULT_OK, intent);
    finish();
}
 
Example 16
Source Project: quickimagepick   File: QiPick.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Use this in {@code onActivityResult(...)} of Activity or Fragment to retrieve image {@link Uri}
 * <br>
 * Note: for Fragments it's advised to use Fragment-specific overloads of this method
 *
 * @param pContext     app {@link Context}
 * @param pRequestCode request code from {@code onActivityResult(...)} parameter
 * @param pResultCode  result code from {@code onActivityResult(...)} parameter
 * @param pData        Intent from {@code onActivityResult(...)} parameter
 * @param pCallback    result callback
 * @return true if result was accepted by QuickImagePick
 * @see #handleActivityResult(Fragment, int, int, Intent, PickCallback)
 * @see #handleActivityResult(android.app.Fragment, int, int, Intent, PickCallback)
 */
@SuppressLint("NewApi")
public static boolean handleActivityResult(@NonNull final Context pContext, final int pRequestCode, final int pResultCode,
                                           @Nullable final Intent pData, @NonNull final PickCallback pCallback) {

    if (pRequestCode != REQ_CAMERA && pRequestCode != REQ_GALLERY && pRequestCode != REQ_DOCUMENTS && pRequestCode != REQ_MULTIPLE) {
        return false;
    }

    final int requestType = PreferenceManager.getDefaultSharedPreferences(pContext)
                                             .getInt(PREFS_REQUEST_TYPE, 0);

    if (pResultCode == Activity.RESULT_OK) {

        if (pRequestCode == REQ_DOCUMENTS) {
            handleResultFromDocuments(requestType, pCallback, pData);
        } else if (pRequestCode == REQ_GALLERY) {
            handleResultFromGallery(requestType, pCallback, pData);
        } else if (pRequestCode == REQ_CAMERA) {
            handleResultFromCamera(pContext, requestType, pCallback, pData);
        } else if (pData == null || pData.getData() == null && API_18 && pData.getClipData() == null) {
            handleResultFromCamera(pContext, requestType, pCallback, pData);
        } else {
            handleResultFromDocuments(requestType, pCallback, pData);
        }

        final String lastCameraUriString = PreferenceManager.getDefaultSharedPreferences(pContext)
                                                            .getString(PREFS_LAST_CAMERA_URI, null);

        final Uri lastCameraUri = TextUtils.isEmpty(lastCameraUriString) ? null : Uri.parse(lastCameraUriString);

        if (lastCameraUri != null) {
            pContext.getApplicationContext()
                    .revokeUriPermission(lastCameraUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }

    } else {

        if (pRequestCode == REQ_DOCUMENTS) {
            pCallback.onCancel(PickSource.DOCUMENTS, requestType);
        } else if (pRequestCode == REQ_GALLERY) {
            pCallback.onCancel(PickSource.GALLERY, requestType);
        } else if (pRequestCode == REQ_CAMERA) {

            pCallback.onCancel(PickSource.CAMERA, requestType);

            deleteLastCameraPic(pContext);

        } else {

            if (pData == null || pData.getData() == null && API_18 && pData.getClipData() == null) {

                pCallback.onCancel(PickSource.CAMERA, requestType);

                deleteLastCameraPic(pContext);

            } else {
                pCallback.onCancel(PickSource.DOCUMENTS, requestType);
            }

        }

    }

    return true;
}
 
Example 17
public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
	if (requestCode == mRequestCodeFilePicker) {
		if (resultCode == Activity.RESULT_OK) {
			if (intent != null) {
				if (mFileUploadCallbackFirst != null) {
					mFileUploadCallbackFirst.onReceiveValue(intent.getData());
					mFileUploadCallbackFirst = null;
				}
				else if (mFileUploadCallbackSecond != null) {
					Uri[] dataUris = null;

					try {
						if (intent.getDataString() != null) {
							dataUris = new Uri[] { Uri.parse(intent.getDataString()) };
						}
						else {
							if (Build.VERSION.SDK_INT >= 16) {
								if (intent.getClipData() != null) {
									final int numSelectedFiles = intent.getClipData().getItemCount();

									dataUris = new Uri[numSelectedFiles];

									for (int i = 0; i < numSelectedFiles; i++) {
										dataUris[i] = intent.getClipData().getItemAt(i).getUri();
									}
								}
							}
						}
					}
					catch (Exception ignored) { }

					mFileUploadCallbackSecond.onReceiveValue(dataUris);
					mFileUploadCallbackSecond = null;
				}
			}
		}
		else {
			if (mFileUploadCallbackFirst != null) {
				mFileUploadCallbackFirst.onReceiveValue(null);
				mFileUploadCallbackFirst = null;
			}
			else if (mFileUploadCallbackSecond != null) {
				mFileUploadCallbackSecond.onReceiveValue(null);
				mFileUploadCallbackSecond = null;
			}
		}
	}
}
 
Example 18
Source Project: AndroidCommons   File: PickImageHandler.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Call this method from {@link Activity#onActivityResult(int, int, Intent)} method to
 * get picked contact info.
 */
@Nullable
public static List<Uri> onResult(@NonNull Context context, int resultCode,
        @Nullable Intent data) {
    if (resultCode != Activity.RESULT_OK || data == null) {
        return null;
    }

    ClipData clip = null;

    if (Build.VERSION.SDK_INT >= 18) {
        clip = data.getClipData();
    }

    final List<Uri> uris;

    if (clip != null) {
        uris = new ArrayList<>();
        for (int i = 0, size = clip.getItemCount(); i < size; i++) {
            uris.add(clip.getItemAt(i).getUri());
        }
    } else if (data.getData() != null) {
        uris = Collections.singletonList(data.getData());
    } else {
        uris = Collections.emptyList();
    }

    // Trying to get persistable read permissions
    if (Build.VERSION.SDK_INT >= 19) {
        final int readFlag = Intent.FLAG_GRANT_READ_URI_PERMISSION;
        if ((data.getFlags() & readFlag) == readFlag) {
            for (Uri uri : uris) {
                try {
                    context.getContentResolver().takePersistableUriPermission(uri, readFlag);
                } catch (Exception ignored) {
                    // Persistable permission was not granted, ignoring
                }
            }
        }
    }

    return uris;
}
 
Example 19
Source Project: Os-FileUp   File: MainActivity.java    License: MIT License 4 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);
    if(Build.VERSION.SDK_INT >= 21){
        Uri[] results = null;

        /*-- if file request cancelled; exited camera. we need to send null value to make future attempts workable --*/
        if (resultCode == Activity.RESULT_CANCELED) {
            if (requestCode == file_req_code) {
                file_path.onReceiveValue(null);
                return;
            }
        }

        /*-- continue if response is positive --*/
        if(resultCode== Activity.RESULT_OK){
            if(requestCode == file_req_code){
                if(null == file_path){
                    return;
                }

                ClipData clipData;
                String stringData;
                try {
                    clipData = intent.getClipData();
                    stringData = intent.getDataString();
                }catch (Exception e){
                    clipData = null;
                    stringData = null;
                }

                if (clipData == null && stringData == null && cam_file_data != null) {
                    results = new Uri[]{Uri.parse(cam_file_data)};
                }else{
                    if (clipData != null) { // checking if multiple files selected or not
                        final int numSelectedFiles = clipData.getItemCount();
                        results = new Uri[numSelectedFiles];
                        for (int i = 0; i < clipData.getItemCount(); i++) {
                            results[i] = clipData.getItemAt(i).getUri();
                        }
                    } else {
                        results = new Uri[]{Uri.parse(stringData)};
                    }
                }
            }
        }
        file_path.onReceiveValue(results);
        file_path = null;
    }else{
        if(requestCode == file_req_code){
            if(null == file_data) return;
            Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
            file_data.onReceiveValue(result);
            file_data = null;
        }
    }
}
 
Example 20
public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
    if (requestCode == mRequestCodeFilePicker) {
        if (resultCode == Activity.RESULT_OK) {
            if (intent != null) {
                if (mFileUploadCallbackFirst != null) {
                    mFileUploadCallbackFirst.onReceiveValue(intent.getData());
                    mFileUploadCallbackFirst = null;
                } else if (mFileUploadCallbackSecond != null) {
                    Uri[] dataUris = null;

                    try {
                        if (intent.getDataString() != null) {
                            dataUris = new Uri[]{Uri.parse(intent.getDataString())};
                        } else {
                            if (Build.VERSION.SDK_INT >= 16) {
                                if (intent.getClipData() != null) {
                                    final int numSelectedFiles = intent.getClipData().getItemCount();

                                    dataUris = new Uri[numSelectedFiles];

                                    for (int i = 0; i < numSelectedFiles; i++) {
                                        dataUris[i] = intent.getClipData().getItemAt(i).getUri();
                                    }
                                }
                            }
                        }
                    } catch (Exception ignored) {
                    }

                    mFileUploadCallbackSecond.onReceiveValue(dataUris);
                    mFileUploadCallbackSecond = null;
                }
            }
        } else {
            if (mFileUploadCallbackFirst != null) {
                mFileUploadCallbackFirst.onReceiveValue(null);
                mFileUploadCallbackFirst = null;
            } else if (mFileUploadCallbackSecond != null) {
                mFileUploadCallbackSecond.onReceiveValue(null);
                mFileUploadCallbackSecond = null;
            }
        }
    }
}