android - 在 Android 中直接将捕获的图像上传到 Cloudinary

标签 android camera image-capture cloudinary

我想拍摄一张照片并直接上传到 Cloudinary。怎么知道图片的名字在上传语句中设置cloudinary.uploader().upload("nameofthepic", Cloudinary.emptyMap()); .

这是我的代码:

public class Camera extends ActionBarActivity implements View.OnClickListener {
    Button b;

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

        b= (Button) findViewById(R.id.button);
        b.setOnClickListener(this);

    }
    public static final int TAKE_PHOTO_REQUEST = 0;

    @Override
    public void onClick(View v) {
        Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);
        Map config = new HashMap();
        config.put("cloud_name", "dkepfkeuu");
        config.put("api_key", "552563677649679");
        config.put("api_secret", "7n8wJ42Hr_6nqZ4aOMDXjTIZ4P0");
        Cloudinary cloudinary = new Cloudinary(config);

        try {
            cloudinary.uploader().upload("", Cloudinary.emptyMap());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

最佳答案

顺序是这样的:
拍照->保存到文件->上传到cloudinary
这是代码:

File photoFile;

     @Override
        public void onClick(View v) {
            Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePhotoIntent.resolveActivity(getPackageManager()) != null) {
                // Create the File where the photo should go
                photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    // Error occurred while creating the File
                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(photoFile));
                    startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                }
            }
        }
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_TAKE_PHOTO) {
            if (resultCode == RESULT_OK) {
                //File to upload to cloudinary
                Map config = new HashMap();
                config.put("cloud_name", "dkepfkeuu");
                config.put("api_key", "552563677649679");
                config.put("api_secret", "7n8wJ42Hr_6nqZ4aOMDXjTIZ4P0");
                Cloudinary cloudinary = new Cloudinary(config);
                try {
                    cloudinary.uploader().upload(photoFile.getAbsolutePath(),          Cloudinary.emptyMap());
                } catch (IOException e) {
                    e.printStackTrace();
                }

            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture
                //finish();
            }
        }
    }

    private File createImageFile() throws IOException {
            // Create an image file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "capturedImage";
            File storageDir = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);
            File image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",         /* suffix */
                    storageDir      /* directory */
            );

            // Save a file: path for use with ACTION_VIEW intents
            return image;
        }

所以你实际上是在上传photoFile这是实际捕获的图像。

很高兴将上传功能与一些图像转换(缩放,调整大小)放在 AsyncTask 中因为现代手机中的相机图像非常大(尺寸)。

希望能帮助到你。

关于android - 在 Android 中直接将捕获的图像上传到 Cloudinary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29044997/

相关文章:

java - 如何在android中对字符串数组进行排序并绑定(bind)在数组列表中

android - 如何检查 Intent ( Activity )的包名称(Espresso)中是否包含字符串?

linux - Linux 上的视频捕获?

android - onTouchEvent onClick onLongClick 调用

Android:为 froyo 及更高版本开发,希望禁用平板电脑支持

java - 我应该导入 android.graphics.camera 还是 android.hardware.camera?

jquery - APP升级IOS后imageURI发生变化

c++ - OpenCV VideoCapture::set() 返回 false 但成功

android - 如何从 Android 中的自定义 CameraView 捕获图像?

java - 在android上根据长值对firebase对象进行排序