我已经在我的应用中的图库中实现了图像选择器选项。从相机中选择拍摄图像后,我将其保存在共享的优先级中,以便用户在再次回到此活动时可以看到该图像。现在我想在另一个活动中使用该图像。但是我不知道如何将那个想象者从那个幻想阶段传递给主要活动。
以下是我的图像选择器代码,并将其保存在共享Preferance中。
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
}
答案 0 :(得分:1)
由于您将其存储在首选项中,因此您可以通过上述注释中提到的首选项访问它。
SharedPreferences preferences = preferenceManager.getDefaultSharedPreferences(this);
String mImageUri = preferences.getString("image", null);
if (mImageUri!=null){
// load image
}
Credits @ vikas-tiwari
答案 1 :(得分:0)
正如您已经建议的那样,您可以使用共享来访问图像。
我给你的建议是创建一个单独的类,您可以在其中创建共享首选项并随时检索实例。
例如:
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);
}
}