上传图片的问题

时间:2014-08-11 01:18:02

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

我已经成功地将用户输入的信息存储到解析中,但是我一直难以将图像存储到解析中,以便能够在以后检索。

为了澄清这一点,下面是我用于配置文件创建活动的代码,这是在用户使用社交媒体进行签名并响应一系列问题(如年龄,首选名称和这些信息)后提示的活动稍后在解析数据库中更新并添加到当前用户。

同样在活动中,用户已经能够从他们的设备上传图片,我的问题现在解决了上传图片并将其存储在解析中,并将图像与使用他们的社交进行签名的当前用户相关联媒体帐户 - 社交媒体集成是通过解析完成的。

我已经查看了以下指南,但仍然非常困惑。https://www.parse.com/docs/android_guide#files

现在我已经尝试完成此操作,并在下面的代码中留下了我的评论。

非常感谢任何帮助。提前致谢。

public class ProfileCreation extends Activity {

    private static final int RESULT_LOAD_IMAGE = 1;
    FrameLayout layout;
    Button save;
    protected EditText mName;
    protected EditText mAge;
    protected EditText mHeadline;
    protected ImageView mprofilePicture;

    protected Button mConfirm;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_creation);

        RelativeLayout v = (RelativeLayout) findViewById(R.id.main); 

        v.requestFocus();

        Parse.initialize(this, "ID", "ID");

        mName = (EditText)findViewById(R.id.etxtname);
        mAge = (EditText)findViewById(R.id.etxtage);
        mHeadline = (EditText)findViewById(R.id.etxtheadline);
        mprofilePicture = (ImageView)findViewById(R.id.profilePicturePreview);

        mConfirm = (Button)findViewById(R.id.btnConfirm);
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String name = mName.getText().toString();
                String age = mAge.getText().toString();
                String headline = mHeadline.getText().toString();



                age = age.trim();
                name = name.trim();
                headline = headline.trim();

                if (age.isEmpty() || name.isEmpty() || headline.isEmpty()) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                    builder.setMessage(R.string.signup_error_message)
                        .setTitle(R.string.signup_error_title)
                        .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
                else {
                    // create the new user!
                    setProgressBarIndeterminateVisibility(true);

                    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(),
                            /*fron 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 "ImageName" and set the string
                    currentUser.put("ImageName", "AndroidBegin Logo");

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

                    // Create the class and the columns
                    currentUser.saveInBackground();

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



        SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
        final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub 
                seekBarValue.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

        }); // Add this

        Button mcancel = (Button)findViewById(R.id.btnBack);
        mcancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ProfileCreation.this.startActivity(new Intent(ProfileCreation.this, LoginActivity.class));
            }
        });




        SeekBar seekBarMinimum = (SeekBar) findViewById(R.id.seekBarMinimumAge);
        final TextView txtMinimum = (TextView) findViewById(R.id.tMinAge);

        seekBarMinimum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub 
                txtMinimum.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

        }); // Add this

        SeekBar seekBarMaximum = (SeekBar) findViewById(R.id.seekBarMaximumAge);
        final TextView txtMaximum = (TextView) findViewById(R.id.tMaxAge);

        seekBarMaximum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub 
                txtMaximum.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

        }); // Add this




        Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });

    } 

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                    && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            }

        }

        private byte[] readInFile(String path) throws IOException {
            // TODO Auto-generated method stub
            byte[] data = null;
            File file = new File(path);
            InputStream input_stream = new BufferedInputStream(new FileInputStream(
                    file));
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            data = new byte[16384]; // 16K
            int bytes_read;
            while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, bytes_read);
            }
            input_stream.close();
            return buffer.toByteArray();

        }
    }

1 个答案:

答案 0 :(得分:1)

在将文件添加到用户之前,您需要等待文件完成保存。

尝试在file.saveInBackground()调用中添加完成块,并将所有currentUser操作移动到该完成块中。