【问题标题】:upload captured image to cloudinary in android将捕获的图像上传到android中的cloudinary
【发布时间】:2015-05-24 12:06:18
【问题描述】:

我想上传一个捕获的图片图像 cloudinary 我在这个声明中有一个错误:

                cloudinary.uploader().upload(is, Cloudinary.emptyMap());

java.lang.NoClassDefFoundError: org.apache.commons.lang.StringUtils

我想问我应该通过什么来定义图片的名称

首先我获取 uri 并将其转换为字符串以获取路径 然后将此真实路径转换为 ​​InputStream 所以,我可以把它传递给云端上传声明

 private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
TextView tv;
String s="aa";
Map config = new HashMap();
Cloudinary cloudinary;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera);
    config.put("cloud_name", "dkepfkeuu");
    config.put("api_key", "key");
    config.put("api_secret", "secret");
    cloudinary = new Cloudinary(config);
    tv=(TextView)findViewById(R.id.textView);
     this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });
}
Bitmap photo;
InputStream is;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);
        s= data.getDataString();

        Toast.makeText(this, "Image saved to:\n" +
                data.getData(), Toast.LENGTH_LONG).show();
        Uri uripath= data.getData();
        String uri = getRealPathFromURI( uripath);
        try {
             is = new FileInputStream(uri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Toast.makeText(this, "uri:\n" +
                uri, Toast.LENGTH_LONG).show();
        tv.setText(s+"---"+uri);
        try {
            cloudinary.uploader().upload(is, Cloudinary.emptyMap());
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
public String getRealPathFromURI(Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor =getContentResolver().query(contentUri,  proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

}

【问题讨论】:

  • 请注意,您的api_secret 绝不应透露给任何人。通过在此处显示您的 api_secret,您的帐户很容易受到滥用。您应该尽快进入您的帐户设置页面并创建一对新的api key+secret

标签: android camera inputstream image-uploading cloudinary


【解决方案1】:

你不需要将Uri转换成字符串,我在上传代码中做了以下操作:

InputStream in = getContentResolver().openInputStream(profileImageUri);
CloudinaryClient.upload(in, profileImageName);

CloudinaryClient:

public static void upload(final InputStream inputStream, final String publicId) {

    final Map<String, String> options = new HashMap<>();
    options.put("public_id", publicId);

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {
                cloudinary.uploader().upload(inputStream, options);
            } catch (IOException e) {
                //TODO: better error handling when image uploading fails
                e.printStackTrace();
            }
        }
    };

    new Thread(runnable).start();
}

注意:您可以将 null 作为选项传递,我只是用它来指定我正在上传的图像的名称。

【讨论】:

  • 是否需要扩展 AsyncTask 并覆盖 (doInBackground) 方法来上传图片??
  • 我已经更新了我的代码,我正在使用一个可运行的,因为我没有给用户任何反馈。
  • 它抛出 Unable to resolve host "api.cloudinary.com": No address associated with hostname 异常。有什么想法吗?
猜你喜欢
  • 2015-05-16
  • 2021-03-20
  • 2019-09-21
  • 2017-08-04
  • 2018-07-29
  • 2015-02-21
  • 2017-07-12
  • 2014-11-25
  • 2019-01-13
相关资源
最近更新 更多