我正在进行一个过滤器活动,该活动的底部有3张图像。默认情况下,将选择图片3,因为它是最后一次被点击的图片。
同一活动包含纬度和经度,这些纬度和经度将被发送到下一个活动以获取附近的位置。
我面临的问题是,当我为图像1选择过滤器并选择图像2时,在这3张图像中,活动重新开始,因此,纬度和经度值分别重置为0.0和0.0
代码-
public class MainActivity extends AppCompatActivity implements FiltersListFragment.FiltersListFragmentListener, EditImageFragment.EditImageFragmentListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_activity_main_filter);
instagramFilterIntent = new Intent(this, MainActivity.class);
ButterKnife.bind(this);
getBundleValues();
loadImage();
setupViewPager(viewPager);
setBitmap(originalImage);
ivSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveImageToGallery();
startingActivtity();
}
});
ivClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveImageToGallery();
}
});
}
double latitude,longitude;
private void getBundleValues() {
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.containsKey("image")) {
Log.e("image", extras.getString("image"));
editingImage = extras.getString("image");
imgFile = new File(extras.getString("image"));
originalFile = imgFile;
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
//Drawable d = new BitmapDrawable(getResources(), myBitmap);
originalImage = myBitmap;
}
}
if(extras.containsKey("PESDK")){
initiated=extras.getBoolean("PESDK");
}
if(extras.containsKey("Latitude")){
latitude=extras.getDouble("Latitude");
Toast.makeText(this, "lat"+latitude, Toast.LENGTH_SHORT).show();
}
if(extras.containsKey("Longitude")){
longitude=extras.getDouble("Longitude");
}
if (extras.containsKey("Filters")) {
filters = extras.getBoolean("Filters");
instagramFilterIntent.putExtra("Filters", filters);
}
if (extras.containsKey("Image1")) {
files[0] = extras.getString("Image1");
instagramFilterIntent.putExtra("Image1", files[0]);
}
if (extras.containsKey("Image2")) {
files[1] = extras.getString("Image2");
instagramFilterIntent.putExtra("Image2", files[1]);
}
if (extras.containsKey("Image3")) {
files[2] = extras.getString("Image3");
instagramFilterIntent.putExtra("Image3", files[2]);
}
initializeImages();
}
}
private void initializeImages() {
if (files[0] != null) {
//Glide.with(getApplicationContext()).load(files[0]).into(preview1Img);
Bitmap bitmap = BitmapFactory.decodeFile(files[0]);
preview1Img.setImageBitmap(bitmap);
if(editingImage.equalsIgnoreCase(files[0])) {
preview1B.setVisibility(View.VISIBLE);
}
}
else {
preview1Layout.setVisibility(View.GONE);
preview1B.setVisibility(View.INVISIBLE);
}
if (files[1] != null) {
//Glide.with(getApplicationContext()).load(files[1]).into(preview2Img);
Bitmap bitmap = BitmapFactory.decodeFile(files[1]);
preview2Img.setImageBitmap(bitmap);
if(editingImage.equalsIgnoreCase(files[1])) {
preview2B.setVisibility(View.VISIBLE);
}
}
else {
preview2Layout.setVisibility(View.GONE);
preview2B.setVisibility(View.INVISIBLE);
}
if (files[2] != null) {
//Glide.with(getApplicationContext()).load(files[2]).into(preview3Img);
Bitmap bitmap = BitmapFactory.decodeFile(files[2]);
preview3Img.setImageBitmap(bitmap);
if(editingImage.equalsIgnoreCase(files[2])){
preview3B.setVisibility(View.VISIBLE);
}
}
else {
preview3Layout.setVisibility(View.GONE);
preview3B.setVisibility(View.INVISIBLE);
}
}
public static void setBitmap(Bitmap originalImage) {
ORIGINAL = originalImage;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
// adding filter list fragment
filtersListFragment = new FiltersListFragment();
filtersListFragment.setInitiatedPESDK(initiated);
filtersListFragment.setListener(this);
// adding edit image fragment
editImageFragment = new EditImageFragment();
editImageFragment.setListener(this);
adapter.addFragment(filtersListFragment, getString(R.string.tab_filters));
adapter.addFragment(editImageFragment, getString(R.string.tab_edit));
//filtersListFragment.prepareThumbnail(originalImage);
viewPager.setAdapter(adapter);
}
@Override
public void onFilterSelected(Filter filter, ImageFilter imageFilter) {
// reset image controls
resetControls();
// applying the selected filter
filteredImage = originalImage.copy(Bitmap.Config.ARGB_8888, true);
// preview filtered image
if(filter!=null) {
imagePreview.setImageBitmap(filter.processFilter(filteredImage));
}else{
imagePreview.setImageBitmap(imageFilter.renderImage(filteredImage,false));
}
finalImage = filteredImage.copy(Bitmap.Config.ARGB_8888, true);
}
/**
* Resets image edit controls to normal when new filter
* is selected
*/
private void resetControls() {
if (editImageFragment != null) {
editImageFragment.resetControls();
}
brightnessFinal = 10;
saturationFinal = 1.0f;
contrastFinal = 1.0f;
}
/**
* Image View click listners
* */
public void image1Click(View view) {
if (!imgFile.toString().equalsIgnoreCase(files[0])) {
saveImageToGallery();
instagramFilterIntent.putExtra("image", files[0]);
instagramFilterIntent.putExtra("PESDK",initiated);
this.finish();
startActivity(instagramFilterIntent);
Toast.makeText(this, "lat"+latitude, Toast.LENGTH_SHORT).show();
}
}
public void image2Click(View view) {
if (!imgFile.toString().equalsIgnoreCase(files[1])) {
saveImageToGallery();
instagramFilterIntent.putExtra("image", files[1]);
instagramFilterIntent.putExtra("PESDK",initiated);
this.finish();
startActivity(instagramFilterIntent);
Toast.makeText(this, "lat"+latitude, Toast.LENGTH_SHORT).show();
}
}
public void image3Click(View view) {
if (!imgFile.toString().equalsIgnoreCase(files[2])) {
saveImageToGallery();
instagramFilterIntent.putExtra("image", files[2]);
instagramFilterIntent.putExtra("PESDK",initiated);
this.finish();
startActivity(instagramFilterIntent);
Toast.makeText(this, "lat"+latitude, Toast.LENGTH_SHORT).show();
}
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
// load the default image from assets on app launch
private void loadImage() {
//@todo
// originalImage = BitmapUtils.getBitmapFromGallery(this, Uri.parse(IMAGE_NAME), 300, 300);
filteredImage = originalImage.copy(Bitmap.Config.ARGB_8888, true);
finalImage = originalImage.copy(Bitmap.Config.ARGB_8888, true);
imagePreview.setImageBitmap(originalImage);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == SELECT_GALLERY_IMAGE) {
Log.e("data.getData()", data.getData() + "");
Bitmap bitmap = BitmapUtils.getBitmapFromGallery(this, data.getData(), 800, 800);
// clear bitmap memory
originalImage.recycle();
finalImage.recycle();
finalImage.recycle();
originalImage = bitmap.copy(Bitmap.Config.ARGB_8888, true);
filteredImage = originalImage.copy(Bitmap.Config.ARGB_8888, true);
finalImage = originalImage.copy(Bitmap.Config.ARGB_8888, true);
imagePreview.setImageBitmap(originalImage);
bitmap.recycle();
// render selected image thumbnails
filtersListFragment.prepareThumbnail(originalImage);
}
}
/*
* saves image to camera gallery
* */
private void saveImageToGallery() {
Dexter.withActivity(this).withPermissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
if (report.areAllPermissionsGranted()) {
if (originalFile.exists()) {
originalFile.delete();
}
// final String path = BitmapUtils.insertImage(getContentResolver(), finalImage, System.currentTimeMillis() + "_profile.jpg", null);
new MainActivity.SaveImageTask(finalImage, originalFile).execute();
//finish();
}
else {
Toast.makeText(getApplicationContext(), "Permissions are not granted!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
token.continuePermissionRequest();
}
}).check();
}
private class SaveImageTask extends AsyncTask<byte[], Void, Void> {
Bitmap finalBitmap;
File name;
public SaveImageTask(Bitmap finalBitmap, File name) {
this.finalBitmap = finalBitmap;
this.name = name;
}
@Override
protected Void doInBackground(byte[]... data) {
FileOutputStream outStream = null;
// Write to SD Card
try {
File file = name;
if (file.exists()) {
} else {
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Log.e("FIlter MainACTIVITY", "SAVED");
Toast.makeText(getApplicationContext(), "Image Saved", Toast.LENGTH_SHORT).show();
}
}
public void startingActivtity(){
Intent chekInActivity = new Intent(MainActivity.this, CurrentLocationPlaces.class);
if(files[0] != null) {
chekInActivity.putExtra("Image1",files[0].toString());
}
if(files[1] != null) {
chekInActivity.putExtra("Image2",files[1].toString());
}
if(files[2] != null) {
chekInActivity.putExtra("Image3",files[2].toString());
}
chekInActivity.putExtra("source","gallery");
chekInActivity.putExtra("Latitude", latitude);
chekInActivity.putExtra("Longitude", longitude);
startActivity(chekInActivity);
}
}
我可以知道如何停止重新启动活动并选择图像以获取预览以应用过滤器,或者如何保存经纬度和经度值,以使活动重新启动时不会被重置?
答案 0 :(得分:0)
如果该值应在重新启动活动时保存,则应使用活动或共享首选项。
基本上在您的onCreate中,加载值并将其分配给Textview(或其他组件)。当它们更改时,只需更新首选项值即可。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Load the lat/lng
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
double lat = preferences.getDouble("latitude", 0);
double lon = preferences.getDouble("longitude", 0);
}
然后,在您的应用程序有更新的地方,只需更新首选项值即可。
private void updatePreference(double lat, double lng) {
SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("latitude", lat);
editor.putInt("longitude", lon);
editor.commit();
}
这将保留您的值,并允许您在活动重新启动时更新/与它们交互。