2016-09-28 56 views
0

因此,我目前正在尝试创建一个按钮,启动相机应用程序,然后将图像上传到解析服务器,但它似乎无法正确上传图像,它崩溃,但它不会给我一个错误输出,不知道为什么它可能试图将文件作为图像上传。我的代码如下。 `私人文件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; 
     } 
    } 
}` 

回答

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

希望它有帮助。

+0

哇,那样做了。谢谢! –