yo2man

7. Retrieving/Downloading the Images from Parse

Sep 19th, 2015
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.68 KB | None | 0 0
  1. /*7.1
  2. We can upload cat videos and pictures of food until
  3. the cows come home, but nobody can view them yet.
  4. So let's fix that.
  5.  
  6. We're going to see how to retrieve data from our back end on parse.
  7. And then we'll see how to view photos and videos from the inbox.
  8. */
  9.  
  10. // We'll want the inbox refreshed every time it's displayed.
  11. // So we'll add code inside the onResume method in FriendsFragment.java
  12. //We are going to query the message class/table but we only want messages that match our User ID //We only want to get messages sent to us
  13.  
  14. // Parse Query, and what it is:
  15. // In many cases, getInBackground isn't powerful enough to specify
  16. which objects you want to retrieve. The ParseQuery offers different ways to retrieve a list of objects rather than just a single object. https://parse.com/docs/android/guide#queries
  17.  
  18.  
  19.    protected List<ParseObject> mMessages; //[*M*]
  20.  
  21. /7.1  We'll want the inbox refreshed every time it's displayed.
  22.     // So we'll add code inside the onResume method.
  23.     @Override
  24.     public void onResume() {
  25.         //We are going to query the message class/table but we only want messages that match our User ID //We only want to get messages sent to us
  26.         super.onResume();
  27.  
  28.         getActivity().setProgressBarIndeterminateVisibility(true);
  29.  
  30.         ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(ParseConstants.CLASS_MESSAGES);
  31.  
  32.         //where clause: (if you are familiar with SQL, you'll know this), it desognates how to search through a collection
  33.         // we want to find all messages, WHERE(.whereEqualTo()) our current user ID is of of the recipients.
  34.         query.whereEqualTo(ParseConstants.KEY_RECIPIENT_IDS, ParseUser.getCurrentUser().getObjectId()); //ParseUser.getCurrentUser().getObjectId() = gets the current user's ID
  35.         query.addDescendingOrder(ParseConstants.KEY_CREATED_AT);   //make the most recent messages at the top
  36.         query.findInBackground(new FindCallback<ParseObject>() { //runs the query in the background
  37.             @Override
  38.             public void done(List<ParseObject> messages, ParseException e) {
  39.                 getActivity().setProgressBarIndeterminate(false); //dismiss the progressbar indicator
  40.  
  41.                 if (e == null) { //if exceptions == null:
  42.                     //We found messages!
  43.                     mMessages = messages; //[*M*]
  44.  
  45.                     // copied over from FriendsFragment.java
  46.                     String[] usernames = new String[mMessages.size()];
  47.                     int i = 0;
  48.                     for (ParseObject message : mMessages) {
  49.                         usernames[i] = message.getString(ParseConstants.KEY_SENDER_NAME);
  50.                         i++;
  51.                     }
  52.                     //ArrayAdapter to adapt to the list*
  53.                     ArrayAdapter<String> adapter = new ArrayAdapter<String>(
  54.                             getListView().getContext(),
  55.                             android.R.layout.simple_list_item_1,
  56.                             usernames);
  57.                     setListAdapter(adapter);
  58.                 }
  59.  
  60.             }
  61.         });
  62.     }
  63.  
  64. // 7.3 Creating a custom Layout for messages
  65. Create a custom layout so that the messages show "Photo" icon or "Video" icon next to the messages.
  66.  
  67. // Step 1. Import the icon images into drawable
  68. // Step 2. Create custom list view adapter.
  69.     -adapter has two parts:
  70.     1. First, we need to define a custom layout that will be used for each item in the list.
  71.     2. Then we need to create a custom class that adapts one message of our Parse objects into the layout.
  72.  
  73.    
  74.     New >Layout file > RelativeLayout, name it: message_item
  75.  
  76. //and then change the layout paddings and stuff a little to make it look good. //Simple
  77.  
  78.  
  79.  
  80. // 7.4 Create a custom list adapter
  81. package com.joinedit.ribbitpractice;
  82.  
  83. import android.content.Context;
  84. import android.view.LayoutInflater;
  85. import android.view.View;
  86. import android.view.ViewGroup;
  87. import android.widget.ArrayAdapter;
  88. import android.widget.ImageView;
  89. import android.widget.TextView;
  90. import com.parse.ParseObject;
  91. import java.util.List;
  92.  
  93. public class MessageAdapter extends ArrayAdapter<ParseObject> {
  94.  
  95.     protected Context mContext;
  96.     protected List<ParseObject> mMessages;
  97.  
  98.     //7.4 create custom layout for messages
  99.     public MessageAdapter(Context context, List<ParseObject> messages) {
  100.         super(context, R.layout.message_item);
  101.         mContext = context;
  102.         mMessages = messages;
  103.     }
  104.  
  105.  
  106.     //7.4 Override getView() in order to use a custom list adapter.
  107.     // for  adapters for fragments, the adapter called a certain method to get an appropriate fragment.
  108.     // And then it went ahead and adapted it and put it in the view.
  109.     //This adapter, which is going to be attached to a list view,
  110.     // is going to call a method, called getView,
  111.     // and it's going to create the view, inflate it into a layout and then attach it into the list view.
  112.     @Override
  113.     public View getView(int position, View convertView, ViewGroup parent) {
  114.         //7.4 we want to do getView as efficiently as possible because the more efficient we are in this method, the faster it will perform
  115.         ViewHolder holder;
  116.  
  117.         //RecyclerView (recycle views if it already exists.)
  118.         if (convertView == null) { //if the view holder does not exist: create new holders
  119.             convertView = LayoutInflater.from(mContext).inflate(R.layout.message_item, null);
  120.             holder = new ViewHolder();
  121.             holder.iconImageView = (ImageView) convertView.findViewById(R.id.messageIcon); //if you do (ImageView)findViewById(R.id.messageIcon) it will be RED error. need "convertView." in front
  122.             //^  find view by ID is an activity method.
  123.             //But we can call it from the convert view because in this  instance, we want to get it for the layout for this view.
  124.             //Because we're not attached to the activity yet.
  125.  
  126.             //do the same thing for TextView holder:
  127.             holder.nameLabel = (TextView) convertView.findViewById(R.id.senderLabel);
  128.         } else { // else if it already exits: reuse the old holders
  129.             holder = (ViewHolder)convertView.getTag(); //.getTag() method gets the holder that was already created
  130.         }
  131.  
  132.         //7.4 Now that we have the view, get the ParseObject that correspond to each item on the list:
  133.         ParseObject message = mMessages.get(position); //get the positon of the message
  134.  
  135.         //7.4 change icon of the message in layout based on message type
  136.         if (message.getString(ParseConstants.KEY_FILE_TYPE).equals(ParseConstants.TYPE_IMAGE)){ //if image file type ".equals" image then:
  137.  
  138.             holder.iconImageView.setImageResource(R.drawable.ic_action_picture); //set message Icon
  139.  
  140.         } else { //else set it to image icon for video:
  141.  
  142.             holder.iconImageView.setImageResource(R.drawable.ic_action_play_over_video);
  143.  
  144.         }
  145.         holder.nameLabel.setText(message.getString(ParseConstants.KEY_SENDER_NAME)); //set message sender name
  146.  
  147.         return convertView;
  148.     }
  149.  
  150.     //7.4 Create a ViewHolder class to use viewHolder (android convention)
  151.     private static class ViewHolder {
  152.         //7.4 a very simple class that just includes the data that is going to be displayed in the custom layout.
  153.         ImageView iconImageView;
  154.         TextView nameLabel;
  155.  
  156.     }
  157. }
  158.  
  159. //--------------------------------------------------------------------------------------
  160.  
  161. // 7.6 View the messages (picture or videos)
  162. // Present a new activity who's entire layout is a single image view to show the image when you click on a message(item) in the list
  163.  
  164. // And n the spirit of having messages that self destruct, we'll add a timer that finishes the activity after ten seconds.
  165.  
  166. // Step 1. New > Activity > Blank Activity
  167. Name it ViewImageActivity
  168.  
  169. package com.joinedit.ribbitpractice;
  170.  
  171. import android.net.Uri;
  172. import android.os.Bundle;
  173. import android.support.v7.app.AppCompatActivity;
  174. import android.view.Menu;
  175. import android.view.MenuItem;
  176. import android.widget.ImageView;
  177.  
  178. public class ViewImageActivity extends AppCompatActivity {
  179.  
  180.     @Override
  181.     protected void onCreate(Bundle savedInstanceState) {
  182.         super.onCreate(savedInstanceState);
  183.         setContentView(R.layout.activity_view_image);
  184.  
  185.  
  186.         ImageView imageView = (ImageView)findViewById(R.id.imageView); //set it to the imageView in activity_view_image.xml
  187.         Uri imageUri = getIntent().getData(); //get the Uri that was passed by the intent in InboxFragment: intent.setData(fileUri);
  188.     }
  189.  
  190.     @Override
  191.     public boolean onCreateOptionsMenu(Menu menu) {
  192.         // Inflate the menu; this adds items to the action bar if it is present.
  193.         getMenuInflater().inflate(R.menu.menu_view_image, menu);
  194.         return true;
  195.     }
  196.  
  197.     @Override
  198.     public boolean onOptionsItemSelected(MenuItem item) {
  199.         // Handle action bar item clicks here. The action bar will
  200.         // automatically handle clicks on the Home/Up button, so long
  201.         // as you specify a parent activity in AndroidManifest.xml.
  202.         int id = item.getItemId();
  203.  
  204.         //noinspection SimplifiableIfStatement
  205.         if (id == R.id.action_settings) {
  206.             return true;
  207.         }
  208.  
  209.         return super.onOptionsItemSelected(item);
  210.     }
  211. }
  212.  
  213.  
  214.  
  215. // 7.7 Using Picasso to View Images from the Web
  216. // can't use the android sdk's imageView method to view images from the web because that must be done asynchronously.
  217. // So use Picasso
  218. // Essentially use this:
  219. Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
  220.  
  221. //simple.
  222.  
  223.  
  224.     @Override
  225.     protected void onCreate(Bundle savedInstanceState) {
  226.         super.onCreate(savedInstanceState);
  227.         setContentView(R.layout.activity_view_image);
  228.  
  229.  
  230.         ImageView imageView = (ImageView)findViewById(R.id.imageView); //set it to the imageView in activity_view_image.xml
  231.         Uri imageUri = getIntent().getData(); //get the Uri that was passed by the intent in InboxFragment: intent.setData(fileUri);
  232.         Picasso.with(this).load(imageUri.toString()).into(imageView); // using Picasso is easy, they have an easy way of passing in the context: "Picasso.with(this)"
  233.         //^this line basically says, "Hey Picasso, with the current context, load this image (uri) into this imageView.
  234.     }
  235.  
  236. //7.8 Viewing Videos
  237. // A video intent that sends the user to their video player they have on their phone to view the video
  238. // super easy to set up
  239.  
  240. // In InboxFragment.java under
  241. // public void onListItemClick
  242.         //7.7 view the video
  243.             Intent intent = new Intent(Intent.ACTION_VIEW, fileUri); //view the file uri
  244.             intent.setDataAndType(fileUri, "video/*");
  245.             startActivity(intent);
Add Comment
Please, Sign In to add comment