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); 
    } 
} 
相关问题