在拍照后上传图片进行解析

时间:2016-09-28 02:10:58

标签: android android-camera android-bitmap

所以我现在正在尝试创建一个启动相机应用程序的按钮然后拍摄图片并将其上传到解析服务器,但是它似乎无法正确上传图片,但它崩溃但它没有#39 ; t给我一个错误输出,不知道为什么它可能试图将文件上传为图像。我的代码如下。 `private File imageFile;     TextView imageFilePath;     EditText editTxt;

String selectedVenue;
String id;
String currentUser;
ParseGeoPoint location;
String venueType;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gen_post);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    editTxt = (EditText) findViewById(R.id.txtInput);

    Intent i = getIntent();
    location = new ParseGeoPoint(i.getDoubleExtra("Lat",0.0),i.getDoubleExtra("Lon",0.0));
    currentUser= ParseUser.getCurrentUser().getUsername();

}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    onBackPressed();
    return true;
}

public void uploadComment(View view){
    String input = editTxt.getText().toString();
    Log.i("AppInfo", "uploading");
    if(input.equals("")|| input.equals(null)){
        Toast.makeText(this,"You must write something",Toast.LENGTH_SHORT);
    }
    else{
        ParseObject object = new ParseObject("UserCommentary");
        if(imageFile != null){
            Bitmap bitmapImage = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
            ByteArrayOutputStream oStream = new ByteArrayOutputStream();
            bitmapImage.compress(Bitmap.CompressFormat.JPEG, 50, oStream);
            byte[] byteArray = oStream.toByteArray();
            object.put("comment", input);
            object.put("Location",location);
            object.put("image",true);
            object.put("imgFile",byteArray);
        }
        else {
            object.put("comment", input);
            object.put("Location",location);
            object.put("image",false);
        }
        object.saveInBackground();
    }
    onBackPressed();


}

public void takePhoto(View view) {

    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), currentUser+".jpeg");
    Uri tempuri = Uri.fromFile(imageFile);
    takePicture.putExtra(MediaStore.EXTRA_OUTPUT, tempuri);
    takePicture.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
    startActivityForResult(takePicture, 0);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == 0){
        switch (resultCode){
            case Activity.RESULT_OK:
                if(imageFile.exists()){
                    imageFilePath.setText(imageFile.getAbsolutePath());

                }
                else{
                    Toast.makeText(this,"Error taking picture",Toast.LENGTH_LONG).show();
                }
                break;
            case Activity.RESULT_CANCELED:
                break;
            default :
                break;
        }
    }
}`

1 个答案:

答案 0 :(得分:0)

以下是如何在Parse云上上传图像的教程。

http://www.androidbegin.com/tutorial/android-parse-com-image-upload-tutorial/

从您的实施和教程之间的比较来看,我认为您缺少以下代码:

// Create the ParseFile passing byte array (image)
ParseFile file = new ParseFile("androidbegin.png", image); 
// Upload the image into Parse Cloud
file.saveInBackground();

然后使用此解析文件存储在解析对象中,如下所示:

object.put("imgFile",file);

希望它有所帮助。