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

Posted

技术标签:

【中文标题】在 Android 中直接将捕获的图像上传到 Cloudinary【英文标题】:upload captured image to Cloudinary directly in Android 【发布时间】:2015-05-16 16:31:33 【问题描述】:

我想拍一张照片直接上传到 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();
        
    

【问题讨论】:

如果下面的答案对您有用,您能否接受它作为标记的答案,以便其他用户相信它有效? 【参考方案1】:

顺序是这样的: 拍照->保存到文件->上传到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 中放置上传功能和一些图像转换(缩放、调整大小)很好,因为现代手机中的相机图像非常大(尺寸)。

希望对你有帮助。

【讨论】:

m 在线获取抽象方法错误:::: cloudinary.uploader().upload(photoFile.getAbsolutePath(), Cloudinary.emptyMap()); 自己解决了 :) Cloudinary.emptyMap() 现在被 ObjectUtils.emptyMap() 取代

以上是关于在 Android 中直接将捕获的图像上传到 Cloudinary的主要内容,如果未能解决你的问题,请参考以下文章

android减少摄像头捕获图像的文件大小小于500 kb

从活动中调用相机,捕获图像并上传到服务器

android如何将捕获的图像保存到手机库中

捕获图像而不将其存储到内部/外部存储

Android将图像从图库上传到服务器[关闭]

如何直接从 Web 应用程序中的内置网络摄像头捕获图像?