拍照后上传图片进行解析

Uploading an image to parse after taking a photo

所以我目前正在尝试创建一个按钮来启动相机应用程序,然后拍摄图像并将其上传到解析服务器,但是它似乎无法正确上传图像,它崩溃了但没有给我一个错误输出,不确定为什么它可能会尝试将文件作为图像上传。我的代码如下。 ` 私有文件图像文件; TextView 图像文件路径; 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;
        }
    }
}`

这是一个关于如何在 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);

希望对您有所帮助。