5

मैं कैमरे का उपयोग करके एक तस्वीर ले सकता हूं या गैलरी से ले सकता हूं और इसे इस कोड का उपयोग करके एक छवि दृश्य में दिखा सकता हूं। मुझे अब क्या करना है उस तस्वीर का उपयोग करना और इसे पार्स पर अपलोड करना है। मैं ऐसा करने के लिए यहां और वहां जा रहा हूं, और मुझे ऐसा करने का सही तरीका नहीं मिला है। क्या कोई भी मेरी इस के साथ मदद कर सकता है? क्या ImageView से छवि अपलोड करना संभव है? धन्यवाद।एंड्रॉइड में पार्स में छवि कैसे अपलोड करें?

protected Button mFromCamera; 
protected Button mFromGallery; 
protected ImageView mImageView; 

private static final int CAMERA_REQUEST = 1888; 
private static final int SELECT_PHOTO = 100; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 


//Initialize ImageView 
mImageView = (ImageView) findViewById(R.id.ImgPrev); 
//Initialize Camera 
mFromCamera = (Button) findViewById(R.id.FromCamera); 

//use camera 
mFromCamera.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(cameraIntent, CAMERA_REQUEST); 
    } //use camera end 

}); 

//initialize button 
mFromGallery = (Button) findViewById(R.id.FromGallery); 

//pick a photo 
mFromGallery.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
     Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     photoPickerIntent.setType("image/*"); 
     startActivityForResult(photoPickerIntent, SELECT_PHOTO); 
    } 
});//pick a photo end 
} 



//previewing Image 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 
switch (requestCode) { 
    //from the gallery 
    case SELECT_PHOTO: 
     if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && null!= data) { 
      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String picturePath = cursor.getString(columnIndex); 
      cursor.close(); 

      mImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
     } 
     break; 
    //from the camera 
    case CAMERA_REQUEST: 
     if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      mImageView.setImageBitmap(photo); 
     } 
     break; 
} 
}//Preview Image End 

उत्तर

2

अपने जवाब पढ़ने:

मैं पहले से ही कोड इससे पहले कि आप है कि पीछा किया। मैं पार्स पर छवि अपलोड करने में सक्षम था। लेकिन मुझे नहीं पता कि ड्रॉइंग स्रोत को कैमरे/गैलरी या इमेजव्यू से मेरी छवि कैसे बदलें। - स्टेनली सैंटोसो

रहे हैं:

अभिषेक बंसल

मैं समझता हूँ कि आपकी समस्या को अपनी छवि को पार्स नहीं कर रहा है?

अपने सवाल का जवाब देने की कोशिश करने के लिए:

मुझे पता है कि drawable स्रोत स्विच करने के लिए कैमरा/गैलरी या imageView से मेरी छवि होने के लिए नहीं है।

1 - R.drawable.androidbegin आपकी समस्या हो रहा है, लेकिन सच है कि आप पहले से ही अपने कोड में पार्स करने के लिए अपने बिटमैप है कि है:

गैलरी से

->

mImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

कैमरे से ->

Bitmap photo = (Bitmap) data.getExtras().get("data"); 

2 - तो मैं करूंगा सु ggest अपने कोड की शुरुआत

private Bitmap yourbitmap; 

3 में प्रकार बिटमैप के एक चर घोषित करने के लिए - तो अपने कोड में गैलरी के लिए बिटमैप और कैमरे निर्दिष्ट करेंगे और इसका इस्तेमाल करते हैं यह पार्स करने के लिए।

... 
yourbitmap = BitmapFactory.decodeFile(picturePath); 
... 
yourbitmap = (Bitmap) data.getExtras().get("data"); 
... 

4 - अंत में आप ऐसा तरह अपने बिटमैप का उपयोग कर सकते हैं:

// Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
//       R.drawable.androidbegin); 
    // Convert it to byte 
        ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
        // Compress image to lower quality scale 1 - 100 
        yourbitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
        byte[] image = stream.toByteArray(); 
      ... 
+0

बहुत बहुत धन्यवाद। यह काम। मुझे एहसास नहीं हुआ कि वे मेरी छवियों का स्रोत हैं क्योंकि प्रारूप बहुत अलग दिखता है। मैं नीचे दिए गए ATActivityForResult के अंदर मामले के तहत अपलोडिंग कोड डाल सकता था। क्या यह एक्टिविटीफॉर रिसेट पर बाहर करना संभव है? मुझे लगता है कि मुझे इसे चालू करने की ज़रूरत है ताकि मैं एक क्लिक में अन्य जानकारी (उपयोगकर्ता नाम और सामान) को पार्स कर सकूं ताकि वे उसी ऑब्जेक्ट/क्लास पर जा सकें। और इस कोड का अर्थ है, अगर मैंने गैलरी से एक छवि ली है, तो मेरे दिमाग को बदले में एक तस्वीर लेने के लिए बदल दिया है, मैं 2 छवियों को अपलोड कर दूंगा। मैं इसे कैसे कर सकता हूं इसलिए मैं केवल 1 छवि अपलोड करता हूं? –

+0

नमस्ते, हाँ आप अपने कोड में अपलोडिंग कोड का उपयोग कर सकते हैं, फिर आप जहां भी चाहें इसे संदर्भित कर सकते हैं। उदाहरण के लिए: निजी myUploading() {// इसे बाइट ByteArrayOutputStream स्ट्रीम = नया ByteArrayOutputStream() में कनवर्ट करें; // कम गुणवत्ता वाले पैमाने पर छवि को संपीड़ित करें 1 - 100 yourbitmap.compress (बिटमैप.कॉमप्रेसफॉर्मैट.पीएनजी, 100, स्ट्रीम); बाइट [] image = stream.toByteArray(); ...} और आपके ब्रेक से पहले एक्टिविटी रिसेट में; आप अपलोड करने के लिए एक कॉल करते हैं: myUploading(); Voilà। – mourad

+0

मेरा जवाब भी था? मैं इतना हरा निशान चाहता था। – mourad

2

इंटरनेट पर अच्छे ट्यूटोरियल उपलब्ध हैं। अनिवार्य रूप से निम्नलिखित आप क्या करने की जरूरत है

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
         R.drawable.androidbegin); 
       // Convert it to byte 
       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       // Compress image to lower quality scale 1 - 100 
       bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
       byte[] image = stream.toByteArray(); 

       // Create the ParseFile 
       ParseFile file = new ParseFile("androidbegin.png", image); 
       // Upload the image into Parse Cloud 
       file.saveInBackground(); 

       // Create a New Class called "ImageUpload" in Parse 
       ParseObject imgupload = new ParseObject("ImageUpload"); 

       // Create a column named "ImageName" and set the string 
       imgupload.put("ImageName", "AndroidBegin Logo"); 

       // Create a column named "ImageFile" and insert the image 
       imgupload.put("ImageFile", file); 

       // Create the class and the columns 
       imgupload.saveInBackground(); 

स्रोत: this tutorial

भी इस सवाल का How to upload an image in parse server using parse api in android

+0

मैं पहले से ही कोड इससे पहले कि आप है कि पीछा किया। मैं पार्स पर छवि अपलोड करने में सक्षम था। लेकिन मुझे नहीं पता कि ड्रॉइंग स्रोत को कैमरे/गैलरी या इमेजव्यू से मेरी छवि कैसे बदलें। –

0

AsyncHttpClient पुस्तकालय हो और आपका चित्र अपलोड करने के लिए क्लिक करें here देखते हैं। यह आपकी छवि अपलोड करने के लिए वसा है।

public void uploadImage(Bitmap img_bit) 
{ 
    AsyncHttpClient imgupload = new AsyncHttpClient(); 
    RequestParams params = new RequestParams(); 
     if (img_bit != null) { 

      byte[] imagebyte; 
      ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
      img_bit.compress(Bitmap.CompressFormat.PNG, 100, bao); 
      imagebyte = bao.toByteArray(); 
      params.put("image", new ByteArrayInputStream(imagebyte), "test"+ System.currentTimeMillis() + ".png"); 
     } 
     imgupload.post("url",params, new AsyncHttpResponseHandler() { 

          @Override 
          public void onSuccess(int arg0, Header[] arg1, 
            byte[] arg2) { 
           System.out.println("Image Upload successfully"); 
          } 

          @Override 
          public void onFailure(int arg0, Header[] arg1, 
            byte[] arg2, Throwable arg3) { 
           System.out.println("faile the data"); 
          } 
         }); 


    } 
संबंधित मुद्दे