2014-06-06 9 views
6

में फ़ोटो और वीडियो को सहेजना और पुनर्प्राप्त करना Parse Android docs पर देख रहा था और फ़ोटो और वीडियो को सहेजने के लिए आपको देखा गया था, आपको डेटा के नाम और बाइट [] के साथ new ParseFile प्रारंभ करना होगा और इसे सहेजना होगा।पार्स (एंड्रॉइड)

छवि उरी और वीडियो उरी को बाइट सरणी में परिवर्तित करने का सबसे आसान तरीका क्या है?

यहाँ मेरी प्रयास किया समाधान हैं:

mPhoto = new ParseFile("img", convertImageToBytes(Uri.parse(mPhotoUri))); 
mVideo = new ParseFile ("vid", convertVideoToBytes(Uri.parse(mVideoUri))); 

private byte[] convertImageToBytes(Uri uri){ 
    byte[] data = null; 
    try { 
     ContentResolver cr = getBaseContext().getContentResolver(); 
     InputStream inputStream = cr.openInputStream(uri); 
     Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
     data = baos.toByteArray(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    return data; 
} 

private byte[] convertVideoToBytes(Uri uri){ 
    byte[] videoBytes = null; 
    try { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     FileInputStream fis = new FileInputStream(new File(getRealPathFromURI(this, uri))); 

     byte[] buf = new byte[1024]; 
     int n; 
     while (-1 != (n = fis.read(buf))) 
      baos.write(buf, 0, n); 

     videoBytes = baos.toByteArray(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return videoBytes; 
} 

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

convertImageToBytes और convertVideoToBytes तरीके अब के लिए काम करते हैं, लेकिन मैं सिर्फ अगर मैं कर सही ढंग से इस कर रहा हूँ सोच रहा हूँ।

+0

वहाँ एक कारण है कि आप चित्र को डीकोड और उसे फिर से सेक है? – harism

+0

@harism मुझे सच में यकीन नहीं है - मुझे लगता है कि मुझे लगता है कि यह एक स्टैक ओवरफ़्लो उत्तर पर है। मुझे वास्तव में इसके साथ बहुत कम अनुभव है, लेकिन ऐसा इसलिए हो सकता है क्योंकि आपको छवि पथ को बिटमैप में परिवर्तित करना होगा। – lschlessinger

+0

बस सोच रहा है क्योंकि यह मामला बहुत अच्छी तरह से हो सकता है कि अलग छवि और वीडियो बाइट सरणी पढ़ने के तरीकों की आवश्यकता नहीं है। – harism

उत्तर

7

उरी से प्राप्त करने के लिए बाइट [] मैं क्या निम्नलिखित बातें,

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
FileInputStream fis = new FileInputStream(new File(yourUri)); 

byte[] buf = new byte[1024]; 
int n; 
while (-1 != (n = fis.read(buf))) 
    baos.write(buf, 0, n); 

byte[] videoBytes = baos.toByteArray(); //this is the video in bytes. 
+0

वीडियो फ़ाइल को बाइट सरणी में परिवर्तित करते समय वीडियो फ़ाइल के आकार को कम करने की कोई संभावना है? कृपया उत्तर दें :( –

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