2015-08-21 80 views
1

我想在我的聊天应用程序中使用Cloudinary服务器上传图片,我已经阅读了关于上传图片的文档,我也注册了我自己在这个Cloudinary服务器上并获取api密钥,密钥,云名称和环境。 以下是我的图片加载代码。我如何在Android上使用cloudinary上传图片

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.  

回答

0

首先,需要注意的是公开透露出你Cloudinary凭证(尤其是你的API_SECRET)强烈不推荐,因为它可以被用来破坏你现有的内容。您可以通过account settings page消除这些密钥对并生成一个新密钥对。

此外,您目睹的问题似乎源自您的依赖关系配置。看到这个帖子:NoClassDefFoundError : org.apache.commons.lang.StringUtils