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

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

本文介绍了将捕获的图像直接在Android中上传到Cloudinary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要拍摄照片并直接将其上传到Cloudinary。我如何知道图片的名称设置在上传语句 cloudinary.uploader()。upload(nameofthepic,Cloudinary.emptyMap()); 。 / p>

这是我的代码:

  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
这里是代码:

 文件photoFile; 

@Override
public void onClick(View v){
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePhotoIntent.resolveActivity(getPackageManager())!= null){
//创建照片应该去的文件
photoFile = null;
try {
photoFile = createImageFile();
} catch(IOException ex){
//创建文件时出错
}
//仅当文件成功创建时继续
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){
//上传到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){
//用户取消了捕获的图像
// finish();
}
}
}

private文件createImageFile()throws IOException {
//创建图像文件名
String timeStamp = new SimpleDateFormat(yyyyMMdd_HHmmss)。format(new Date());
String imageFileName =capturedImage;
文件storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName,/ * prefix * /
.jpg,/ * suffix * /
storageDir / *目录* /
);

//保存文件:与ACTION_VIEW意图一起使用的路径
return image;
}

因此,您实际上上传的是 photoFile

AsyncTask中放置上传函数以及一些图像转换(缩放,调整大小)



希望它有帮助。


I want to capture a picture and upload it to Cloudinary directly. How can I know the name of the pic to set it at the upload statement cloudinary.uploader().upload("nameofthepic", Cloudinary.emptyMap());.

Here is my code:

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();
        }
    }
}
解决方案

The order is like this:Take picture->save to file->upload to cloudinaryHere is the code:

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

So you are actually uploading photoFile which is the actually captured image.

Its nice to put the upload function along with some image transformations(scale,resize) in AsyncTask because camera images in modern handsets are quite big(in size).

Hope it helps.

这篇关于将捕获的图像直接在Android中上传到Cloudinary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 08:50