내 응용 프로그램의 갤러리에서 이미지 선택기 옵션을 구현했습니다. 갤러리에서 사진을 찍은 후 카메라에서 사진을 찍은 후 다시 공유 할 때이 이미지를 볼 수 있도록 공유 우선 순위로 유지합니다. 이제 다른 활동에서 그 이미지를 사용하고 싶습니다. 그러나 나는 어떻게 그 imagr를 저 주요 클래스에서 주요 활동으로 넘어갈 수 있을지 모른다.한 액티비티에서 다른 액티비티로 공유 우선권을 통해 이미지 변경
여기 이미지 선택기에 대한 코드이며 공유 우선 순위로 유지하십시오.
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);
}
}
그리고 이것은 내가
편집 코드
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