handle a couple more Pick intents

This commit is contained in:
tibbi 2016-06-27 15:40:25 +02:00
parent 75e4f6c8ab
commit 971141652b
2 changed files with 33 additions and 13 deletions

View file

@ -23,12 +23,10 @@
<intent-filter> <intent-filter>
<action android:name="android.intent.action.PICK"/> <action android:name="android.intent.action.PICK"/>
<category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="vnd.android.cursor.dir/image"/>
</intent-filter>
<intent-filter> <data android:mimeType="image/*"/>
<action android:name="android.intent.action.PICK"/> <data android:mimeType="video/*"/>
<category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="vnd.android.cursor.dir/image"/>
<data android:mimeType="vnd.android.cursor.dir/video"/> <data android:mimeType="vnd.android.cursor.dir/video"/>
</intent-filter> </intent-filter>

View file

@ -79,6 +79,7 @@ public class MainActivity extends AppCompatActivity
mIsGetImageContentIntent = isGetImageContentIntent(intent); mIsGetImageContentIntent = isGetImageContentIntent(intent);
mIsGetVideoContentIntent = isGetVideoContentIntent(intent); mIsGetVideoContentIntent = isGetVideoContentIntent(intent);
mIsSetWallpaperIntent = isSetWallpaperIntent(intent); mIsSetWallpaperIntent = isSetWallpaperIntent(intent);
mIsThirdPartyIntent = mIsPickImageIntent || mIsPickVideoIntent || mIsGetImageContentIntent || mIsGetVideoContentIntent || mIsThirdPartyIntent = mIsPickImageIntent || mIsPickVideoIntent || mIsGetImageContentIntent || mIsGetVideoContentIntent ||
mIsSetWallpaperIntent; mIsSetWallpaperIntent;
} }
@ -356,20 +357,20 @@ public class MainActivity extends AppCompatActivity
} }
private boolean isPickImageIntent(Intent intent) { private boolean isPickImageIntent(Intent intent) {
return isPickIntent(intent) && intent.getData().equals(MediaStore.Images.Media.EXTERNAL_CONTENT_URI); return isPickIntent(intent) && (hasImageContentData(intent) || isImageType(intent));
} }
private boolean isPickVideoIntent(Intent intent) { private boolean isPickVideoIntent(Intent intent) {
return isPickIntent(intent) && intent.getData().equals(MediaStore.Video.Media.EXTERNAL_CONTENT_URI); return isPickIntent(intent) && (hasVideoContentData(intent) || isVideoType(intent));
} }
private boolean isPickIntent(Intent intent) { private boolean isPickIntent(Intent intent) {
return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_PICK) && intent.getData() != null; return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_PICK);
} }
private boolean isGetContentIntent(Intent intent) { private boolean isGetContentIntent(Intent intent) {
return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_GET_CONTENT) && return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_GET_CONTENT) &&
!intent.getType().isEmpty(); intent.getType() != null;
} }
private boolean isGetImageContentIntent(Intent intent) { private boolean isGetImageContentIntent(Intent intent) {
@ -386,19 +387,40 @@ public class MainActivity extends AppCompatActivity
return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_SET_WALLPAPER); return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_SET_WALLPAPER);
} }
private boolean hasImageContentData(Intent intent) {
final Uri data = intent.getData();
return data != null && data.equals(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
}
private boolean hasVideoContentData(Intent intent) {
final Uri data = intent.getData();
return data != null && data.equals(MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
}
private boolean isImageType(Intent intent) {
final String type = intent.getType();
return type != null && (type.startsWith("image/") || type.equals(MediaStore.Images.Media.CONTENT_TYPE));
}
private boolean isVideoType(Intent intent) {
final String type = intent.getType();
return type != null && (type.startsWith("video/") || type.equals(MediaStore.Video.Media.CONTENT_TYPE));
}
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
if (requestCode == PICK_MEDIA && data != null) { if (requestCode == PICK_MEDIA && data != null) {
final Intent result = new Intent(); final Intent result = new Intent();
if (mIsGetImageContentIntent || mIsGetVideoContentIntent) {
final String path = data.getData().getPath(); final String path = data.getData().getPath();
final Uri uri = Uri.fromFile(new File(path)); final Uri uri = Uri.fromFile(new File(path));
if (mIsGetImageContentIntent || mIsGetVideoContentIntent) {
final String type = Utils.getMimeType(path); final String type = Utils.getMimeType(path);
result.setDataAndTypeAndNormalize(uri, type); result.setDataAndTypeAndNormalize(uri, type);
result.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); result.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else { } else if (mIsPickImageIntent || mIsPickVideoIntent) {
result.setData(data.getData()); result.setData(uri);
result.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} }
setResult(RESULT_OK, result); setResult(RESULT_OK, result);