2011-09-06 14 views
9

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

तो मैं अपने पूरे एप्लिकेशन में उस बिटमैप्स को स्टोर करना चाहता हूं।

क्या कोई मेरी मदद कर सकता है? मैं इस पर बहुत फंस गया हूँ।

धन्यवाद।

+0

आप इसके माध्यम से जा सकते हैं। यह निश्चित रूप से आपकी सहायता करेगा। [यहां क्लिक करें] [1] [1]: http://stackoverflow.com/questions/17268519/how-to-store-bitmap-object-in-sharedpreferences-in-android –

उत्तर

11

अरे मित्र मैं अपनी समस्या यहाँ के समाधान मिल गया मैं अपने कोड पोस्ट ताकि दूसरों इस समाधान का उपयोग कर सकते हैं ..

1)। बटन पर क्लिक करें - छवि

ContentValues values = new ContentValues(); 
values.put(MediaStore.Images.Media.TITLE, fileName); 
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 

Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE"); 
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); 
startActivityForResult(cameraIntent, CAMERA_REQUEST); 

2) पर कब्जा करने के लिए खुला कैमरा। बटन क्ल्लिक पर - चयनित छवि के लिए खुली गैलरी

Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT); 
galleryintent.setType("image/*"); 
startActivityForResult(galleryintent, IMAGE_PICK); 

3)। स्टेटिक चर

private static final int CAMERA_REQUEST = 0; 
    private static final int IMAGE_PICK = 1; 

4)। onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
     { 
      switch(requestCode) 
      { 
       case CAMERA_REQUEST: 
        if(resultCode == RESULT_OK) 
        { 
         String[] projection = { MediaStore.Images.Media.DATA}; 
         Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
         int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
         cursor.moveToFirst(); 
         String capturedImageFilePath = cursor.getString(column_index_data); 
         Log.d("photos*******"," in camera take int "+capturedImageFilePath); 

         Bitmap photo_camera = BitmapFactory.decodeFile(capturedImageFilePath, options); 

         if(data != null) 
         { 
    img_1.setImageBitmap(photo_camera); 
           prefsEditor.putString(Global.PHOTO_1,capturedImageFilePath); 
    } 
    } 
case IMAGE_PICK: 
       if(resultCode == RESULT_OK) 
       { 
        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 filePath = cursor.getString(columnIndex); 
        cursor.close(); 

//     Bitmap photo = BitmapFactory.decodeFile(filePath); 
        Bitmap photo_gallery = BitmapFactory.decodeFile(filePath,options); 
        img_1.setImageBitmap(photo_gallery); 
         prefsEditor.putString(Global.PHOTO_1, filePath); 
} 

} 
     prefsEditor.commit(); 
} 

5)। ऑनस्ट्रोय() में आपको सेट किए गए सभी बिटमैप को नष्ट करना होगा।

@Override 
    public void onDestroy() 
    { 
     super.onDestroy(); 
     if(photo_camera != null) 
     { 
      photo_camera.recycle(); 
     } 
     if(photo_gallery != null) 
     { 
      photo_gallery.recycle(); 
     } 
} 

6)। उस समय जब आप साझा किए गए डेटा से डेटा प्राप्त करते हैं तो आपको स्ट्रिंग को बिटमैप में कनवर्ट करना होगा और फिर आप ImageView में बिटमैप सेट कर सकते हैं। उदाहरण के लिए , बिटमैप बिट 1 = बिटमैपफैक्टरी.decodeFile (strimg1); और फिर सेट करें, imageView.setImageBitmap

+0

आपकी समस्या के समाधान पोस्ट करने के लिए धन्यवाद। अनुत्तरित सूची से इसे हटाने के लिए चयनित के रूप में अपना उत्तर सेट करें। – blessenm

+0

हे @anddev क्या आप अपना पूरा कोड पोस्ट कर सकते हैं ?? –

2

आप इस तरह SharedPreference में मान जोड़ सकते हैं:

SharedPreferences pref = getSharedPreferences("abc", 0); 
Editor edit = pref.edit(); 
edit.putBoolean(arg0, arg1); 
edit.putFloat(arg0, arg1); 
edit.putInt(arg0, arg1); 
edit.putLong(arg0, arg1); 
edit.putString(arg0, arg1); 
edit.commit(); 

आप SharedPreference में केवल Boolean, Float, Int, Long, String मूल्यों जोड़ सकते हैं।

छवि को स्टोर करने के लिए आपको डिवाइस की बाहरी या आंतरिक मेमोरी चाहिए।

+0

धन्यवाद, मैं प्रयोग किया जाता है जैसा कि आपने बताया था। लेकिन अब मैं स्ट्रिंग को बिटमैप में अपनी छवि प्रदर्शित करने से कनवर्ट करना चाहता हूं। क्या स्ट्रिंग टू बिटमैप को बदलने के लिए आपके पास कोई विचार हो सकता है? – anddev

+0

इसे आजमाएं: http: // stackoverflow।कॉम/प्रश्न/5405373/कनवर्टिंग-स्ट्रिंग-ए-बिट-बिट-इन-एंड्रॉइड –

3

साझा साझािकरण में बिटमैप्स स्टोर न करें। यदि आपको अपने आवेदन के जीवनकाल के दौरान इसे जारी रखने की आवश्यकता है, तो आप इसे static फ़ील्ड में असाइन कर सकते हैं। यदि आप डिवाइस रीबूट पर भी इसे जारी रखना चाहते हैं, तो उसे फ़ाइल में या डेटाबेस में रखें।

अधिक जानकारी के लिए पढ़ने के http://developer.android.com/resources/faq/framework.html#3

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

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