Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  2. Button galleryBtn = new Button(getActivity());
  3. galleryBtn.setText("Import Photo...");
  4. galleryBtn.setOnClickListener(v -> {
  5. Intent intent = new Intent();
  6. intent.setAction(Intent.ACTION_GET_CONTENT);
  7. intent.setType("image/*");
  8. this.startActivityForResult(intent, 0);
  9. });
  10.  
  11. //... some more code here
  12. }
  13.  
  14. //... some more code here
  15.  
  16. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  17. if (requestCode == 0)
  18. try {
  19. if (data != null) {
  20. InputStream stream = getActivity().getContentResolver().openInputStream(
  21. data.getData());
  22. imageBitmap = BitmapFactory.decodeStream(stream);
  23. stream.close();
  24.  
  25. imageView.setImageBitmap(imageBitmap);
  26. }
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31.  
  32. imageBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
  33.  
  34. public class ImageBox {
  35. public static Queue<Bitmap> mQ = new LinkedBlockingQueue<Bitmap>();
  36. }
  37.  
  38. private void downloadFile(final String url){
  39. mExecutorService.submit(new Runnable() {
  40. @Override
  41. public void run() {
  42. Bitmap b = BitmapFromURL.getBitmapFromURL(url);
  43. synchronized (this){
  44. TaskCount--;
  45. }
  46. Intent i = new Intent(ACTION_ON_GET_IMAGE);
  47. ImageBox.mQ.offer(b);
  48. sendBroadcast(i);
  49. if(TaskCount<=0)stopSelf();
  50. }
  51. });
  52. }
  53.  
  54. private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  55. public void onReceive(Context context, Intent intent) {
  56. LOG.d(TAG, "BroadcastReceiver get broadcast");
  57.  
  58. String action = intent.getAction();
  59. if (DownLoadImageService.ACTION_ON_GET_IMAGE.equals(action)) {
  60. Bitmap b = ImageBox.mQ.poll();
  61. if(b==null)return;
  62. if(mListener!=null)mListener.OnGetImage(b);
  63. }
  64. }
  65. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement