2015-03-14 4 views
5

पर क्लाउडिनरी पर कैप्चर की गई छवि अपलोड करें, मैं एक तस्वीर कैप्चर करना चाहता हूं और इसे क्लाउडिनरी पर सीधे अपलोड करना चाहता हूं। मैं इसे अपलोड कथन 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(); 
     } 
    } 
} 
+0

यदि नीचे दिया गया उत्तर आपके लिए काम करता है, तो क्या आप इसे चिह्नित उत्तर के रूप में स्वीकार कर सकते हैं ताकि अन्य उपयोगकर्ताओं को विश्वास हो कि यह काम करता है? – santafebound

उत्तर

6

क्रम इस तरह है:: ले picture-> फ़ाइल के लिए> अपलोड यहाँ 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 में कुछ छवि परिवर्तन (स्केल, आकार बदलें) के साथ अपलोड फ़ंक्शन को रखना अच्छा लगता है क्योंकि आधुनिक हैंडसेट में कैमरा छवियां काफी बड़ी हैं (आकार में)।

उम्मीद है कि यह मदद करता है।

+0

मीटर लाइन पर अमूर्त विधि त्रुटि प्राप्त कर रहा है :::: cloudinary.uploader() अपलोड करें (photoFile.getAbsolutePath(), Cloudinary.emptyMap()); – Pranita

+1

अपने आप से समाधान मिला :) Cloudinary.emptyMap() अब ObjectUtils.emptyMap() द्वारा प्रतिस्थापित किया गया है – Pranita