Last active
November 17, 2016 19:13
-
-
Save Richie97/a6dee51e6b1e79338a04178288b9232a to your computer and use it in GitHub Desktop.
CommitContentApi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CommitEditText extends AppCompatEditText { | |
private CommitListener commitListener; | |
public CommitEditText(Context context) { | |
super(context); | |
} | |
public CommitEditText(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
public CommitEditText(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
@Override | |
public InputConnection onCreateInputConnection(final EditorInfo info) { | |
final InputConnection ic = super.onCreateInputConnection(info); | |
EditorInfoCompat.setContentMimeTypes(info, new String[]{"image/gif"}); | |
final InputConnectionCompat.OnCommitContentListener callback = new InputConnectionCompat.OnCommitContentListener() { | |
@Override | |
public boolean onCommitContent(InputContentInfoCompat info, int flags, Bundle opts) { | |
if (BuildCompat.isAtLeastNMR1() && (flags & InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) { | |
try { | |
info.requestPermission(); | |
} catch (Exception e) { | |
return false; // return false if failed | |
} | |
} | |
if(commitListener != null){ | |
commitListener.onCommitContent(info.getContentUri()); | |
} | |
info.releasePermission(); | |
return true; // return true if succeeded | |
} | |
}; | |
return InputConnectionCompat.createWrapper(ic, info, callback); | |
} | |
public void setCommitListener(CommitListener listener) { | |
this.commitListener = listener; | |
} | |
public interface CommitListener { | |
void onCommitContent(Uri uri); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
((CommitEditText) findViewById(R.id.edit)).setCommitListener(new CommitEditText.CommitListener() { | |
@Override | |
public void onCommitContent(Uri uri) { | |
ImageView imageView = (ImageView) findViewById(R.id.image); | |
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView); | |
Glide.with(MainActivity.this).load(uri).into(imageViewTarget); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment