Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

android - I want to retrieve the image from parse! Storing the image in parse works! How can i get the image from parse in imageview?

public class Profile extends Activity {

ImageView personal_ImageView;
private static final int SELECT_PICTURE = 100;
private String selectedImagePath;
private ParseObject totem;

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

    personal_ImageView = (ParseImageView) findViewById(R.id.personal_imageView);

    ParseUser currentUser = ParseUser.getCurrentUser();

    String currentUserUsername = currentUser.getUsername();

    Bitmap bitmap = ((BitmapDrawable) personal_ImageView.getDrawable()).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] data = stream.toByteArray();
    ParseFile file = new ParseFile(selectedImagePath, data);
    file.saveInBackground();
    final ParseObject gameScore = new ParseObject("imageProfile");
    gameScore.put("image", file);
    gameScore.put("user", currentUserUsername);
    gameScore.saveInBackground();

    personal_ImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);

            ParseFile file = gameScore.getParseFile("image");
            final ParseImageView imageView = (ParseImageView) findViewById(R.id.personal_imageView);
            imageView.setParseFile(file);
            imageView.loadInBackground(new GetDataCallback() {
                public void done(byte[] data, ParseException e) {
                    if (e == null){
                        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                        if (bitmap != null) {
                            personal_ImageView.setImageBitmap(bitmap);
                        }
                    }
                    // The image is loaded and displayed!
                    int oldHeight = imageView.getHeight();
                    int oldWidth = imageView.getWidth();
                    Log.v("LOG!!!!!!", "imageView height = " + oldHeight);      // DISPLAYS 90 px
                    Log.v("LOG!!!!!!", "imageView width = " + oldWidth);        // DISPLAYS 90 px
                }
            });
        }
    });
}

Please help me guys... I can upload the image to parse but i am not be able to retrieve the image and put it into the parse imageview! What am i doing wrong in the code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you may want to take a look at this:

http://square.github.io/picasso/

Cheers!

Code I used to retrieve image from parse using file URL.

 ParseFile file = message.getParseFile(Insert Key File Here);
 Uri fileUri = Uri.parse(file.getUrl());
 Picasso.with(this).load(imageUri.toString()).into(imageView);

Then you can add a callback to Picasso if you want like so:

Picasso.with(this).load(imageUri.toString()).into(imageView, new    com.squareup.picasso.Callback() {
        @Override
        public void onSuccess() {

            //Success
        }

        @Override
        public void onError() {

         //Error Occurred  

        }
    });    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...