Parse.com&安培;安卓:我如何将图像保存到我的解析数据库,这样我可以把它们作为一个资料图片 [英] Parse.com & Android: How do I save Images to my parse database so that i can use them as a profile picture

查看:164
本文介绍了Parse.com&安培;安卓:我如何将图像保存到我的解析数据库,这样我可以把它们作为一个资料图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的是新以及parse.com。我需要一种方法中,用户可以点击一个按钮来拍照或其他按钮,选择从图库中的照片。然后将图像存储在我的parse.com数据库,这样我可以与系统中的其他事件进行​​操作。到目前为止,我能够让用户更新自己的状态,并保存到我的解析数据库,但我不知道该如何处理图像和ImageViews。这里是我的code迄今:

 公共类UpdateActivity延伸活动{    受保护的EditText mUpdateStatus;
    保护按钮mUpdateStatusButton;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_update);        //变量初始化
        mUpdateStatus =(EditText上)findViewById(R.id.updateStatusUpdate);
        mUpdateStatusButton =(按钮)findViewById(R.id.updateButtonUpdate);        // code更新按钮单击事件
        mUpdateStatusButton.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                //获取当前用户
                ParseUser的currentUser = ParseUser.getCurrentUser(); //标识当前用户
                字符串currentUserUsername = currentUser.getUsername(); //用户名存储在变量                //创建新的变量来存储字符串
                。字符串NEWSTATUS = mUpdateStatus.getText()的toString();                //事件为空状态
                如果(newStatus.isEmpty())
                        {AlertDialog.Builder建设者=新AlertDialog.Builder(UpdateActivity.this);
                        builder.setMessage(地位不应是空的。);
                        builder.setTitle(哎呀!);
                        builder.setPositiveButton(OK,新DialogInterface.OnClickListener(){
                            @覆盖
                            公共无效的onClick(DialogInterface对话,诠释它){
                                dialog.dismiss();
                            }
                        });
                        AlertDialog对话框= builder.create();
                        dialog.show();}
                其他{
                //保存在Parse.com状态
                的parseObject statusObject =新的parseObject(状态); //创建一个新的解析类
                statusObject.put(NEWSTATUS,NEWSTATUS); //创建一个新的属性,并从NEWSTATUS增加价值
                statusObject.put(用户,currentUserUsername); //存储的用户名在新的解析类                //保存数据,并开始回调方法
                statusObject.saveInBackground(新SaveCallback(){
                    @覆盖
                    公共无效完成(ParseException的E){
                        如果(E == NULL)
                            {//事件一个成功的存储
                                Toast.makeText(UpdateActivity.this,的getString(R.string.succssfulUpdate),Toast.LENGTH_LONG).show();                                //将用户返回到个人资料
                                主要意图=新意图(UpdateActivity.this,ProfileActivity.class);
                                UpdateActivity.this.startActivity(主);                            }
                        其他
                            {//事件不成功的存储
                                AlertDialog.Builder建设者=新AlertDialog.Builder(UpdateActivity.this);
                                builder.setMessage(e.getMessage());
                                builder.setTitle(对不起!);
                                builder.setPositiveButton(OK,新DialogInterface.OnClickListener(){
                                    @覆盖
                                    公共无效的onClick(DialogInterface对话,诠释它){
                                        dialog.dismiss();
                                    }
                                });
                                AlertDialog对话框= builder.create();
                                dialog.show();
                            }
                    }
                });}            }
        });    }
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.menu_update,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();        // noinspection SimplifiableIfStatement
        开关(ID){
            案例R.id.logoutUpdateMenu:
            {//注销用户
                ParseUser.logOut();
                //将用户返回到登录
                意向意图=新意图(UpdateActivity.this,LoginActivity.class);
                UpdateActivity.this.startActivity(意向);
                UpdateActivity.this.finish();
                Toast.makeText(getApplicationContext(),的getString(R.string.logout_text),Toast.LENGTH_LONG).show();
                打破;}
            }        返回super.onOptionsItemSelected(项目);
    }
}


解决方案

我有parse.com没有经验,但如果你能够把图像(位图)成的parseObject,你只需要调用拍照或挑照片使用意图和行动startActivityForResult。例如:

 公共无效onClickTakePhoto(查看视图){
    dispatchTakePictureIntent();
}
公共无效onClickPickPhoto(查看视图){
    意图I =新意图(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(ⅰ,REQUEST_SELECT_IMAGE);
}
从Android文档// code
私人无效dispatchTakePictureIntent(){
    意图takePictureIntent =新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
    如果(takePictureIntent.resolveActivity(getPackageManager())!= NULL){
        startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE);
    }
}
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    如果((要求code == || REQUEST_IMAGE_CAPTURE要求code == REQUEST_SELECT_IMAGE)及和放大器;结果code == RESULT_OK){
        捆绑额外= data.getExtras();
        位图imageBitmap =(位图)extras.get(数据);
        的parseObject statusObject =新的parseObject(状态);
        //我觉得解析也有类似的支持,如果不是这
        statusObject.put(profile_photo,imageBitmap);
        statusObject.saveInBackground(新回调(){...});    }
}

I am new at android as well as parse.com. I need a way in which a user can click a button to take a photo or another button to choose a photo from the gallery. Then store the image in my parse.com database so that I can manipulate it with other events in the system. So far I am able to have the user update his status and save to my parse database but I do not know how to manipulate images and ImageViews. Here is my code so far:

public class UpdateActivity extends Activity {

    protected EditText mUpdateStatus;
    protected Button mUpdateStatusButton;


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

        //initialization of variables
        mUpdateStatus=(EditText)findViewById(R.id.updateStatusUpdate);
        mUpdateStatusButton=(Button)findViewById(R.id.updateButtonUpdate);

        //code update button click event
        mUpdateStatusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Get current user
                ParseUser currentUser=ParseUser.getCurrentUser();//Identifies current user
                String currentUserUsername=currentUser.getUsername();//stores username in variable

                //Create new variable to store strings
                String newStatus=mUpdateStatus.getText().toString();

                //Event for an empty status
                if (newStatus.isEmpty())
                        {AlertDialog.Builder builder=new AlertDialog.Builder(UpdateActivity.this);
                        builder.setMessage("STATUS SHOULD NOT BE EMPTY.");
                        builder.setTitle("OOPS!");
                        builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        AlertDialog dialog=builder.create();
                        dialog.show();}
                else{
                //Save the status in Parse.com
                ParseObject statusObject = new ParseObject("Status");//Create a new parse class
                statusObject.put("newStatus",newStatus);//Creates a new attribute and adds value from newStatus
                statusObject.put("User",currentUserUsername);//Stores username in new parse class

                //Save data and initiate callback method
                statusObject.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if(e==null)
                            {//Event for a Successful storage
                                Toast.makeText(UpdateActivity.this,getString(R.string.succssfulUpdate),Toast.LENGTH_LONG).show();

                                //Take user back to profile
                                Intent main = new Intent(UpdateActivity.this, ProfileActivity.class);
                                UpdateActivity.this.startActivity(main);

                            }
                        else
                            {//Event for an Unsuccessful storage
                                AlertDialog.Builder builder=new AlertDialog.Builder(UpdateActivity.this);
                                builder.setMessage(e.getMessage());
                                builder.setTitle("SORRY!");
                                builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                                AlertDialog dialog=builder.create();
                                dialog.show();
                            }
                    }
                });}

            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_update, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch(id) {
            case R.id.logoutUpdateMenu:
            {//logout the user
                ParseUser.logOut();
                //Take user back to login
                Intent intent = new Intent(UpdateActivity.this, LoginActivity.class);
                UpdateActivity.this.startActivity(intent);
                UpdateActivity.this.finish();
                Toast.makeText(getApplicationContext(), getString(R.string.logout_text), Toast.LENGTH_LONG).show();
                break;}
            }

        return super.onOptionsItemSelected(item);
    }
}

解决方案

I have no experience with parse.com but If you are able to put images(bit map) into ParseObject, you just need to call take photo or pick photo action using intent and startActivityForResult. Example:

public void onClickTakePhoto(View view) {
    dispatchTakePictureIntent();
}
public void onClickPickPhoto(View view) {
    Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, REQUEST_SELECT_IMAGE);
}
//Code from Android documentation
private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if ((requestCode == REQUEST_IMAGE_CAPTURE || requestCode == REQUEST_SELECT_IMAGE) && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        ParseObject statusObject = new ParseObject("Status");
        //I think parse has similar support If not this
        statusObject.put("profile_photo",imageBitmap);
        statusObject.saveInBackground( new Callback(){...});

    }
}

这篇关于Parse.com&安培;安卓:我如何将图像保存到我的解析数据库,这样我可以把它们作为一个资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆