上传和保存图像

时间:2014-08-14 14:36:30

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

我在完成这些代码行时遇到了问题:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), /*to be retrieved from image view */);"

我希望不是从我的Drawable而是从ImageView检索getResources,其中从用户图片库中显示imageview的图片(很快用户将能够将图像从facebook上传到他们的imageview中。

简而言之,我试图让用户将他们的图片(个人资料图片)上传到他们的个人资料创建页面的解析中,他们必须填写各种信息。用户可以在图像视图中预览他们的图片。

以下是完整的代码:

ParseUser currentUser = ParseUser.getCurrentUser();

                 /* This is the section where the images is converted, saved, and uploaded. I have not been able Locate the image from the ImageView, where the user uploads the picture to imageview from either their gallery and later on from facebook */ 
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                        /*to be retrieved from image view */);
                // Convert it to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] image = stream.toByteArray();

                // Create the ParseFile
                ParseFile file = new ParseFile("profilePicture.png", image);
                // Upload the image into Parse Cloud
                file.saveinBackground();



                // Create a column named "Profile Picture" and set the string
                currentUser.put("ImageName", "Profile Picture");

                // Create a column named "ImageFile" and insert the image
                currentUser.put("ProfilePicture", file);

                // Create the class and the columns

                currentUser.put("name", name); 
                currentUser.put("age", age); 
                currentUser.put("headline", headline); 
                currentUser.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        setProgressBarIndeterminateVisibility(false);

                        if (e == null) {
                            // Success!
                            Intent intent = new Intent(ProfileCreation.this, MoodActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                        }
                        else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                            builder.setMessage(e.getMessage())
                                .setTitle(R.string.signup_error_title)
                                .setPositiveButton(android.R.string.ok, null);
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    }
                });
            }
        }
    });

以下是整个活动代码

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");


        @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.whereEqualTo("ActivityName", activityName);
               query.whereGreaterThanOrEqualTo("Minimum_Age", minimumAge);
               query.whereLessThanOrEqualTo("Maximum_Age", maximumAge);
        //     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).getUsername().toString());
                        }

                        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("username", 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();
                   }
               }
            });
        }
    }

1 个答案:

答案 0 :(得分:0)

根据您的说法,您可以假设ImageView的基础资源是BitmapDrawable,因此您应该能够像这样:

Bitmap bitmap = ((BitmapDrawable) myImgView.getDrawable()).getBitmap();

如果由于任何原因上述内容不正确,您可以执行以下其他技巧:子类化ImageView覆盖onDraw方法,并将图像绘制在{ {1}}你自己就像这样:

Bitmap

请注意,这不能保证正确显示图像,只是为了复制它......