如何将图像从相机上传到服务器而不是图像选择器

时间:2019-10-13 22:13:59

标签: android image-uploading

如何将图像从相机上传到Web服务器,

我可以使用此tuto中的图像选择器进行上传

https://www.simplifiedcoding.net/android-upload-image-to-server/

,但我想从相机和图库中选择图像

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def init():
    line1, = ax1.plot([], [])
    line2, = ax2.plot([], [])
    line3, = ax3.plot([], [])
    return line1, line2, line3     

def animate(i):
    #show moving sine function in lower subplot
    line1.set_data(x, np.sin(2*np.pi*x + 10*i))
    line1.axes.axis([0, 5, -1, 1])

    #show moving sine function in upper subplot
    line2.set_data(x, np.sin(2*np.pi*x + 10*i))
    line2.axes.axis([0, 5, -1, 1])

    #show moving cosine function in large transparent plot
    line3.set_data(x, np.cos(2*np.pi*x + 10*i))
    line3.axes.axis([0, 5, -1, 1])

    return line1, line2, line3



#create figure and subplots
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.4])
line1, = ax1.plot([], [])
ax2 = fig.add_axes([0.1, 0.5, 0.8, 0.4])
line2, = ax2.plot([], [])

#create axis in front with dimensions that span both subplots
ax3 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
#make background of large axis transparent
ax3.patch.set_alpha(0)
line3, = ax3.plot([], [])


#create x-data
x = np.linspace(0, 5, 100)


ani = animation.FuncAnimation(fig, animate, init_func= init, interval = 200, blit = True) 

我也找到了这种方法,但是它对我不起作用

如何正确替换上面代码中的文件选择器方法以获取图像路径和名称,然后像在tuto中一样将其上传

    public void uploadMultipart() {


        //getting the actual path of the image
        String path = getPath(filePath);

        //Uploading code
        try {
            String uploadId = UUID.randomUUID().toString();

            //Creating a multi part request
            new MultipartUploadRequest(this, uploadId, Constants.UPLOAD_URL)
                    .addFileToUpload(path, "image") //Adding file
                    .addParameter("name", name) //Adding text parameter to the request
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .startUpload(); //Starting the upload

        } catch (Exception exc) {
            Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }


    //method to show file chooser
    private void showFileChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }

    //handling the image chooser activity result
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            filePath = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                imageView.setImageBitmap(bitmap);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


1 个答案:

答案 0 :(得分:0)

要将捕获的图像上传到服务器,整个代码保持不变,您只需要将捕获的图像文件路径传递到uploadMultipart函数中即可。

您将在onActivityResult中获得捕获的图像文件路径。