如何将图片从Android应用程序上传到服务器?

时间:2017-04-02 05:33:39

标签: android file-upload android-camera image-uploading retrofit2

我已经开发了一小部分。它可以拍照只是现在不存储。我想从Android应用程序上传图片(使用相机)到服务器。我使用过改造库,网络服务(asp.net)。我怎么能这样做?

清单:

public class UploadPhotoActivity extends AppCompatActivity {
    static final int REQUEST_IMAGE_CAPTURE = 1;

    ImageView imageView;

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

        imageView=(ImageView)findViewById(R.id.imageView);

    }


    public void SelectPhotoMethod(View view){

        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 && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            imageView.setImageBitmap(imageBitmap);
        }
    }

}

CODE:

#include <conio.h> // put this somewhere up at the top of the file
void intro()
{
    cout << "Stuff being said " << endl;
    cout << "More stuff being said " << endl;
    cout << "Press 'enter' to continue or 'backspace' to skip...";
    int result = _getch(); // returns number equivalent of a character
    if (result == 8) // 8 is the number equivalent of backspace
        return;
    ...
    code
    ...
}

enter image description here

1 个答案:

答案 0 :(得分:2)

  1. 您可以使用MultipartEntityMultiPart图片上传到服务器或
  2. 您还可以使用众所周知的VolleyRetrofit库,以最少的代码将图片上传到服务器。
  3. MultiPart

    Uploading Camera Image, Video to Server with Progress Bar

    Volley或Retrofit

    1. Android Volley Tutorial to Upload Image to Server
    2. Upload Image From Android App Using Retrofit 2
    3. 更多教程:

      1. Upload File To Server
      2. Android Upload Image using Android Upload Service
      3. 希望这会有所帮助〜