我已經在我的應用程序中從圖庫中實現圖像選擇器選項。從照相機中選擇圖像或從相機拍攝照片後,我將其保存在共享的首選項中,以便用戶在他們再次返回到此活動時可以看到該圖像。現在我想在另一個活動中使用該圖像。但我不知道我該如何將這個想象從這個恐怖分子階層傳遞給主要活動。更改圖像通過共享的優先從一個活動到另一個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
}
嘗試您已在ViewProfileFragment活性'SharedPreferences偏好= PreferenceManager.getDefaultSharedPreferences(本)做了相同的; String mImageUri = preferences.getString(「image」,null);'@ tamrezh21 – UltimateDevil
您不需要將圖像傳遞給另一個活動,只需執行與從SharedPreference獲取圖像相同的操作即可。 – RajatN
@VikasTiwari我在主選項中編輯了代碼,但仍然沒有得到任何圖片 – tamrezh21