检索Parse上的文件 - 图像

时间:2014-08-15 21:20:26

标签: java android android-layout android-activity parse-platform

我已经设法将图像文件存储到解析中。我的问题在于检索它。在这种意义上,在注册过程中,要求用户上传个人资料图片,并且稍后将图像存储在其中作为图像进行解析。我的问题在于检索它,除了提取名称,标题,年龄等潜在匹配信息之外,我还希望能够检索图片。

下面是代码,我在其中检索字符串信息,但我也想要检索文件。

public class MatchingActivity extends Activity {

 private String currentUserId;
    private ArrayAdapter<String> namesArrayAdapter;
    private ArrayList<String> names;
    private ListView usersListView;
    private Button logoutButton;
    String userGender = ParseUser.getCurrentUser().getString("Gender");
    String activityName = ParseUser.getCurrentUser().getString("ActivityName");
    Number maxDistance = ParseUser.getCurrentUser().getNumber("Maximum_Distance");


    String userLookingGender = ParseUser.getCurrentUser().getString("Looking_Gender");
    Number minimumAge = ParseUser.getCurrentUser().getNumber("Minimum_Age");
    Number maximumAge = ParseUser.getCurrentUser().getNumber("Maximum_Age");
    Number userage = ParseUser.getCurrentUser().getNumber("Age");



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.matching);

        logoutButton = (Button) findViewById(R.id.logoutButton);
        logoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ParseUser.logOut();
                Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(intent);
            }
        });

        setConversationsList();
    }
    private void setConversationsList() {
        currentUserId = ParseUser.getCurrentUser().getObjectId();
        names = new ArrayList<String>();
      // String userActivitySelectionName = null;

           ParseQuery<ParseUser> query = ParseUser.getQuery();

         //  query.whereEqualTo("ActivityName",userActivitySelectionName);

           query.whereNotEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
           // users with Gender = currentUser.Looking_Gender
           query.whereEqualTo("Gender", userLookingGender);
           // users with Looking_Gender = currentUser.Gender
           query.whereEqualTo("Looking_Gender", userGender);
           query.setLimit(1);
           query.whereEqualTo("ActivityName", activityName);
           query.whereGreaterThanOrEqualTo("Minimum_Age", minimumAge).whereGreaterThanOrEqualTo("Age", userage);
           query.whereLessThanOrEqualTo("Maximum_Age", maximumAge).whereLessThanOrEqualTo("Age", userage);
  //  query.whereWithinKilometers("Maximum_Distance", point, maxDistance)





        query.findInBackground(new FindCallback<ParseUser>() {

            public void done(List<ParseUser> userList, ParseException e) {
                if (e == null) {
                    for (int i=0; i<userList.size(); i++) {
                        names.add(userList.get(i).get("Name").toString());
                        names.add(userList.get(i).get("Headline").toString());
                        names.add(userList.get(i).get("Age").toString());

                 //       names.add(userList.get(i).getParseObject("ProfilePicture").;


                    }




                    usersListView = (ListView)findViewById(R.id.usersListView);
                    namesArrayAdapter =
                        new ArrayAdapter<String>(getApplicationContext(),
                            R.layout.user_list_item, names);
                    usersListView.setAdapter(namesArrayAdapter);

                    usersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> a, View v, int i, long l) {
                            openConversation(names, i);
                        }
                    });

                } else {
                    Toast.makeText(getApplicationContext(),
                        "Error loading user list",
                            Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    public void openConversation(ArrayList<String> names, int pos) {
        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.whereEqualTo("Name", names.get(pos));
        query.findInBackground(new FindCallback<ParseUser>() {
           public void done(List<ParseUser> user, ParseException e) {
               if (e == null) {
                   Intent intent = new Intent(getApplicationContext(), MessagingActivity.class);
                   intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
                   startActivity(intent);
               } else {
                   Toast.makeText(getApplicationContext(),
                       "Error finding that user",
                           Toast.LENGTH_SHORT).show();
               }
           }
        });
    }
}

如果您有任何疑问,请填写。

更新 图片代码

ParseFile image = (ParseFile) userData.getParseFile("ProfilePicture");

                        //call the function 

                        displayImage(image, image_expert);
                        //and here is the function

                            private void displayImage(ParseFile thumbnail, final ImageView img) {

                                if (thumbnail != null) {
                                    thumbnail.getDataInBackground(new GetDataCallback() {

                                        @Override
                                        public void done(byte[] data, ParseException e) {

                                            if (e == null) {
                                                Bitmap bmp = BitmapFactory.decodeByteArray(data, 0,
                                                        data.length);

                                                if (bmp != null) {

                                                    Log.e("parse file ok", " null");
                                                    img.setImageBitmap(Bitmap.createScaledBitmap(bmp,
                                                    (display.getWidth() / 5),
                                                    (display.getWidth() /50), false));
                                                    img.setImageBitmap(getRoundedCornerBitmap(bmp, 10));
                                                    img.setPadding(10, 10, 0, 0);



                                                }
                                            } else {
                                                Log.e("paser after downloade", " null");
                                            }

                                        }
                                    });
                                } else {

                                    Log.e("parse file", " null");

                                    // img.setImageResource(R.drawable.ic_launcher);

                                    img.setPadding(10, 10, 10, 10);
                                }

                            }
                            names.add(userList.get(i).get("Age").toString());
                            names.add(userList.get(i).get("ActivityName").toString());


                        //       names.add(userList.get(i).getParseObject("ProfilePicture").;


                    }

活动代码

public class Fragment1 extends Fragment {

    private String currentUserId;
    private ArrayAdapter<String> namesArrayAdapter;
    private ArrayList<String> names;
    private ListView usersListView;
    private Button logoutButton;
    String userGender = ParseUser.getCurrentUser().getString("Gender");
    String activityName = ParseUser.getCurrentUser().getString("ActivityName");
    Number maxDistance = ParseUser.getCurrentUser().getNumber("Maximum_Distance");
    String userLookingGender = ParseUser.getCurrentUser().getString("Looking_Gender");
    Number minimumAge = ParseUser.getCurrentUser().getNumber("Minimum_Age");
    Number maximumAge = ParseUser.getCurrentUser().getNumber("Maximum_Age");
    Number userage = ParseUser.getCurrentUser().getNumber("Age");

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setConversationsList();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1_layout, container, false);


        return view;
    }

private void setConversationsList() {
    currentUserId = ParseUser.getCurrentUser().getObjectId();
    names = new ArrayList<String>();
    // String userActivitySelectionName = null;

    ParseQuery<ParseUser> query = ParseUser.getQuery();

    //  query.whereEqualTo("ActivityName",userActivitySelectionName);

    query.whereNotEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
    // users with Gender = currentUser.Looking_Gender
    query.whereEqualTo("Gender", userLookingGender);
    // users with Looking_Gender = currentUser.Gender
    query.whereEqualTo("Looking_Gender", userGender);
    query.setLimit(1);
    query.whereEqualTo("ActivityName", activityName);
    //query.whereGreaterThanOrEqualTo("Age", minimumAge);
    //query.whereLessThanOrEqualTo("Age", maximumAge);
    query.orderByDescending("Name");




    query.findInBackground(new FindCallback<ParseUser>() {

        public void done(List<ParseUser> userList, ParseException e) {
            if (e == null) {
                for (int i=0; i<userList.size(); i++) {
                    names.add(userList.get(i).get("Name").toString());
                    names.add(userList.get(i).get("Headline").toString());

                    ParseFile image = (ParseFile) userData.getParseFile("ProfilePicture");

                    //call the function 

                    displayImage(image, image_expert);
                    //and here is the function

                        private void displayImage(ParseFile thumbnail, final ImageView img) {

                            if (thumbnail != null) {
                                thumbnail.getDataInBackground(new GetDataCallback() {

                                    @Override
                                    public void done(byte[] data, ParseException e) {

                                        if (e == null) {
                                            Bitmap bmp = BitmapFactory.decodeByteArray(data, 0,
                                                    data.length);

                                            if (bmp != null) {

                                                Log.e("parse file ok", " null");
                                                img.setImageBitmap(Bitmap.createScaledBitmap(bmp,
                                                (display.getWidth() / 5),
                                                (display.getWidth() /50), false));
                                                img.setImageBitmap(getRoundedCornerBitmap(bmp, 10));
                                                img.setPadding(10, 10, 0, 0);



                                            }
                                        } else {
                                            Log.e("paser after downloade", " null");
                                        }

                                    }
                                });
                            } else {

                                Log.e("parse file", " null");

                                // img.setImageResource(R.drawable.ic_launcher);

                                img.setPadding(10, 10, 10, 10);
                            }

                        }
                        names.add(userList.get(i).get("Age").toString());
                        names.add(userList.get(i).get("ActivityName").toString());


                    //       names.add(userList.get(i).getParseObject("ProfilePicture").;


                }




                usersListView = (ListView)getActivity().findViewById(R.id.userlistview);
                namesArrayAdapter =
                        new ArrayAdapter<String>(getActivity().getApplicationContext(),
                                R.layout.user_list_item, names);
                usersListView.setAdapter(namesArrayAdapter);

                usersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> a, View v, int i, long l) {
                        openConversation(names, i);
                    }
                });

            } else {
                Toast.makeText(getActivity().getApplicationContext(),
                        "Error loading user list",
                        Toast.LENGTH_LONG).show();
            }
        }
    });
}

public void openConversation(ArrayList<String> names, int pos) {
    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.whereEqualTo("Name", names.get(pos));
    query.findInBackground(new FindCallback<ParseUser>() {
        public void done(List<ParseUser> user, ParseException e) {
            if (e == null) {
                Intent intent = new Intent(getActivity().getApplicationContext(), MessagingActivity.class);
                intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
                startActivity(intent);
            } else {
                Toast.makeText(getActivity().getApplicationContext(),
                        "Error finding that user",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });
}
}

以下一行的问题

                private void displayImage(ParseFile thumbnail, final ImageView img) {
Multiple markers at this line
    - Syntax error on token ",", ; 
     expected
    - Syntax error on token "(", ; 
     expected
    - Syntax error on token ")", ; 
     expected

2 个答案:

答案 0 :(得分:1)

看看下面的内容是否会有所帮助:

ParseUser currentUser = ParseUser.getCurrentUser();
    currentUser.fetchInBackground(new GetCallback<ParseObject>() {
        public void done(ParseObject object, ParseException e) {
            if (e == null) {
                ParseFile file = (ParseFile)object.get("image");
                if(file != null){
                    file.getDataInBackground(new GetDataCallback() {
                        public void done(byte[] data, ParseException e) {
                            if (e == null) {
                                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                                if(bitmap != null){
                                    imageAccount.setImageBitmap(bitmap);
                                }else{
                                    Log.d(TAG, "file null?");
                                }
                            } else {
                                Log.d(TAG, "ParseFile ParseException: " + e.toString());
                            }
                        }
                    });
                }else{
                    Log.d(TAG, "ParseFile is null");
                }
            } else {
                Log.d(TAG, "ParseException: " + e.toString());
            }
        }
    });

答案 1 :(得分:0)

当你在parse中存储一个指向'file'类型的指针时,你会得到一个URL和一个名字。

无论何时您引用该文件,您都应该能够获取该URL。

使用URL执行异步获取并生成本地位图以在视图中使用照片。