我如何在android中使用cloudinary上传图像

时间:2015-08-21 11:34:49

标签: android cloudinary

我想在我的聊天应用程序中使用Cloudinary服务器上传图像,我已阅读有关上传图像的文档,我还在此Cloudinary服务器上注册了自己,并获取了api密钥,密钥,云名称和环境。         下面是我卸载图片的代码。

    public class ImageUpload extends Activity
    {
        private static boolean first = true;
    private ProgressDialog dialog = null;
        Cloudinary cloudinary;

        JSONObject Result;
            String file_path = "";
        File file;
        Button btnupload;
        FileInputStream fileInputStream;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.imageupload);
                 Intent i = getIntent();
                btnupload = (Button)findViewById(R.id.btn_upload);
                file_path = "/storage/emulated/0/Contacts_Cover/crop_ContactPhoto-IMG_20150814_152743.jpg";
        HashMap config = new HashMap();
                config.put("cloud_name", "chatgeeta");
                config.put("api_key", "255663733636595");//I have changed the key and secret
                config.put("api_secret", "5Upmclme1ydaTWBi7_COU7uIns0");
                cloudinary = new Cloudinary(config);
              TextView textView = (TextView) findViewById(R.id.textView1);
                textView.setText(file_path);
                file = new File(file_path);
                try {
                     fileInputStream = new FileInputStream(file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
             }
          public class Upload extends AsyncTask<String, Void, String>
        {
            private Cloudinary mCloudinary;

            public Upload( Cloudinary cloudinary ) {
                super();
                mCloudinary = cloudinary;
            }
            @Override
            protected String doInBackground(String... params) {
                String response ="";
                try
                {
                    Result =  (JSONObject) mCloudinary.uploader().upload(fileInputStream, Cloudinary.emptyMap());
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
                return response;
            }

            @Override
            protected void onPostExecute(String result) {
                 TextView textView = (TextView) findViewById(R.id.textView1);
                    textView.setText("file uploaded");
                super.onPostExecute(result);
            }}
        public void upload(View view)
 {
            new Upload( cloudinary ).execute();
    }}

    When i execute above code then i got "Caused by java.lang.NoClassDefFoundError:org.apache.commons.lang.StringUtils" as error.     

1 个答案:

答案 0 :(得分:0)

首先,请注意,不建议公开披露您的Cloudinary凭据(尤其是您的API_SECRET),因为它可能会损坏您的现有内容。您可以通过account settings page删除这些密钥对并生成一个新密钥对。

此外,您目睹的问题似乎源自您的依赖项配置。请参阅此主题:NoClassDefFoundError : org.apache.commons.lang.StringUtils