0

我已經在我的應用程序中從圖庫中實現圖像選擇器選項。從照相機中選擇圖像或從相機拍攝照片後,我將其保存在共享的首選項中,以便用戶在他們再次返回到此活動時可以看到該圖像。現在我想在另一個活動中使用該圖像。但我不知道我該如何將這個想象從這個恐怖分子階層傳遞給主要活動。更改圖像通過共享的優先從一個活動到另一個android

這是我的圖像選擇器的代碼,並保持它共享Prefelents。

public class ViewProfileFragment extends Fragment implements View.OnClickListener{ 


    private ImageView image; 

    private int REQUEST_CAMERA = 0, SELECT_FILE = 1; 

    String userChoosenTask; 
    Bitmap bm; 
    String currentPhotoPath; 
    Uri uri; 

    private String UPLOAD_URL = Constants.HTTP.PHOTO_URL; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View rootView = inflater.inflate(R.layout.fragment_view_profile, container, false); 


     image=(ImageView)rootView.findViewById(R.id.profile_pic); 
     saveData(); 
     return rootView; 
    } 
    public void saveData(){ 
     ..... 
     if (results.size() > 0) { 
      ...... 

      SharedPreferences preferences = 
        PreferenceManager.getDefaultSharedPreferences(getActivity()); 
      String mImageUri = preferences.getString("image", null); 
      if (mImageUri != null) { 
       image.setImageURI(Uri.parse(mImageUri)); 
       System.out.println("imageuri"+Uri.parse(mImageUri)); 
      } else { 
       Glide.with(this) 
         .load(Constants.HTTP.PHOTO_URL+mail) 
         .thumbnail(0.5f) 
         .override(200,200) 
         .diskCacheStrategy(DiskCacheStrategy.ALL) 
         .into(image); 

       System.out.println(Constants.HTTP.PHOTO_URL+mail); 
      } 

    } 


    private void galleryAddPic() { 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     File f = new File(currentPhotoPath); 
     uri = Uri.fromFile(f); 
     mediaScanIntent.setData(uri); 
     this.getActivity().sendBroadcast(mediaScanIntent); 

     SharedPreferences preferences = 
       PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     SharedPreferences.Editor editor = preferences.edit(); 
     editor.putString("image", String.valueOf(uri)); 
     editor.commit(); 

     try { 
      Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri); 
      image.setImageBitmap(bitmap); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void onCaptureImageResult() { 
     Bitmap bitmap = getBitmapFromPath(currentPhotoPath, 200, 200); 
     image.setImageBitmap(bitmap); 
     compressBitMap(bitmap); 
    } 

    private void onSelectFromGalleryResult(Intent data) { 
     uri = data.getData(); 
     String[] projection = {MediaStore.Images.Media.DATA}; 
     Cursor cursor = getContext().getContentResolver().query(uri, projection, null, null, null); 
     if (cursor != null) { 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      currentPhotoPath = cursor.getString(column_index); 
      uri = Uri.fromFile(new File(currentPhotoPath)); 
      cursor.close(); 
     } else { 
      currentPhotoPath = uri.getPath(); 
     } 

     SharedPreferences preferences = 
       PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     SharedPreferences.Editor editor = preferences.edit(); 
     editor.putString("image", String.valueOf(uri)); 
     editor.commit(); 

     try { 
      Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri); 
      image.setImageBitmap(bitmap); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     bm = BitmapFactory.decodeFile(currentPhotoPath); 
     compressBitMap(bm); 
    } 
} 

而這正是我想通過使用sharedprefenace

編輯代碼

public class MainOptionPage extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{ 


    private ImageView imgProfile; 
    RealmResults<MyColleagueModel> results; 

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

     .... 
     // Navigation view header 

     Intent intent = getIntent(); 
     navigationHeader= navigationView.getHeaderView(0); 
     .... 
     imgProfile = navigationHeader.findViewById(R.id.profile_image); 

     // load nav menu header data 
     loadNavHeader(); 

     //setupDrawerContent(navigationView); 

     navigationView.setNavigationItemSelectedListener(this); 
    } 

    private void loadNavHeader() { 

     GlobalClass globalClass = new GlobalClass(); 
     String mEmail = globalClass.getEmail_info(); 

     Realm profileRealm; 
     profileRealm = Realm.getDefaultInstance(); 

     results = profileRealm.where(MyColleagueModel.class).equalTo("mail", mEmail).findAll(); 

     //fetching the data 
     results.load(); 

     // name, website 
     String name=null; 
     String profile_image; 
     byte[] profile_byte = new byte[0]; 

     // if(globalClass.readDatafromStorage().contains("ACTIVATE")) { 
      name = " "; 
      if (results.size() > 0) { 
       name = results.get(0).getName().toString(); 
      } 
      profile_image = globalClass.getImage_urlpath(); 


      profile_image = globalClass.getImage_urlpath(); 
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
     String mImageUri = preferences.getString("image", null); 
     if (mImageUri != null) { 
      imgProfile.setImageURI(Uri.parse(mImageUri)); 

     System.out.println("imageuri"+Uri.parse(mImageUri)); 
     } else { 
      Glide.with(this).load(profile_image) 
        .thumbnail(0.5f) 
        .override(200, 200) 
        .diskCacheStrategy(DiskCacheStrategy.ALL) 
        .into(imgProfile); 
      Log.d("--LoginPage_NAME--", name); 
     } 

     txtName.setText(name); 
     txtWebsite.setText(mEmail); 


     Glide.with(this).load(profile_image) 
       .thumbnail(0.5f) 
       .override(200,200) 
       .diskCacheStrategy(DiskCacheStrategy.ALL) 
       .into(imgProfile); 

     // TODO: 11/9/17 set bitmap in imageview by removing below comment 

    } 
+3

嘗試您已在ViewProfileFragment活性'SharedPreferences偏好= PreferenceManager.getDefaultSharedPreferences(本)做了相同的; String mImageUri = preferences.getString(「image」,null);'@ tamrezh21 – UltimateDevil

+0

您不需要將圖像傳遞給另一個活動,只需執行與從SharedPreference獲取圖像相同的操作即可。 – RajatN

+0

@VikasTiwari我在主選項中編輯了代碼,但仍然沒有得到任何圖片 – tamrezh21

回答

1

既然你將其存儲在首選項,您可以訪問看到圖像的另一項活動它通過上面評論中提到的首選項。

SharedPreferences preferences = preferenceManager.getDefaultSharedPreferences(this); 
String mImageUri = preferences.getString("image", null); 

if (mImageUri!=null){ 
    // load image 
} 

現金@維卡斯 - 蒂瓦里

+0

我已經編輯了代碼,但我無法在MainOptionPage中看到更新的圖像 – tamrezh21

+0

因爲您需要一種方法來刷新圖像。使你的'loadNavHeader()'方法公開,在你的片段內部將你的getActivity()轉換成如下所示的主活動: 'if(getActivity()instanceof MainAcitivty){((MainAcitivity)getActivity())。loadNavHeader()}當它在片段中被改變時它將更新活動中的圖像。不要忘記在將新圖像的uri保存爲首選項後將其添加。 – MadScientist

+1

謝謝您的回答。這是workinng,我會更新這個問題。 – tamrezh21

0

正如你已經指出的,可以使用共享訪問圖像。
我給你的建議是創建一個類,在其中創建sharedpreferences並隨時檢索實例。
實施例:

public class MySharedPreferences { 

    private static MySharedPreferences mInstance; 
    private static Context mCtx; 

    private MySharedPreferences(Context context) { 
     mCtx = context; 
    } 

    public static synchronized MySharedPreferences getInstance(Context context) { 
     if (mInstance == null) { 
      mInstance = new MySharedPreferences(context); 
     } 
     return mInstance; 
    } 


    public boolean saveData(String data1, String data2) { 
     SharedPreferences sharedPreferences = mCtx.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.putString(KEY1, data1); 
     editor.putString(KEY2, data2); 
     return editor.commit(); 
    } 

public String getData1() { 
     SharedPreferences sharedPreferences = mCtx.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
     return sharedPreferences.getString(KEY1, null); 
    } 
} 
相關問題