I have images selected from the gallery displayed in ImageView, But not retaining or saved Image when the android device restart I need to re-pick an image again. My plan is for the image to still stay on the image view even the device is rebooted , or do I need to create some data to save the image and display to ImageView
public class FirstFragment extends Fragment implements View.OnClickListener{
ImageView imageButton1;
private Uri mImageUri;
#Override
public void onResume() {
super.onResume();
}
private File mSnapFile;
private static final String ARG_URI_IMAGE_1 = "image1Uri";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_first, container, false);
imageButton1 = (ImageView) v.findViewById(R.id.firstimagebtn);
imageButton1.setOnClickListener(this::onClick);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String mImageUri = preferences.getString("image", null);
if (mImageUri != null) {
imageButton1.setImageURI(Uri.parse(mImageUri));
}
return v;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.firstimagebtn:
Intent intent;
if (Build.VERSION.SDK_INT < 19) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
} else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
}
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 0);
break;
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 0:
if(resultCode == Activity.RESULT_OK){
if (data != null) {
// This is the key line item, URI specifies the name of the data
mImageUri = data.getData();
// Saves image URI as string to Default Shared Preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("image", String.valueOf(mImageUri));
editor.commit();
// Sets the ImageView with the Image URI
imageButton1.setImageURI(mImageUri);
imageButton1.invalidate();
}
}
break;
case 1:
if(resultCode == Activity.RESULT_OK){
// This is the key line item, URI specifies the name of the data
mImageUri2 = data.getData();
// Saves image URI as string to Default Shared Preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("image2", String.valueOf(mImageUri2));
editor.commit();
// Sets the ImageView with the Image URI
imageButton2.setImageURI(mImageUri2);
imageButton2.invalidate();
}
break;
}
}
You can use image loading libraries like Glide or Picasso. They provide data caching and your image will be persisted.
See docs
By default, Glide checks multiple layers of caches before starting a new request for an image:
1.Active resources - Is this image displayed in another View right now?
2.Memory cache - Was this image recently loaded and still in memory?
3.Resource - Has this image been decoded, transformed, and written to the disk cache before?
4.Data - Was the data this image was obtained from written to the disk cache before?
The first two steps check to see if the resource is in memory and if so, return the image immediately. The second two steps check to see if the image is on disk and return quickly, but asynchronously.
If all four steps fail to find the image, then Glide will go back to the original source to retrieve the data (the original File, Uri, Url etc).
Hope this helps. Let me know if you face an issue with this but I think the docs are pretty much self explanatory.
Related
Hello I want to have an Add function that allows me to input items to my GridView
For Background: I have a standard GridView and an XML activity (which contains 2 TextView) that I want to convert to my GridView. I also have a custom ArrayAdapter class and custom Word object (takes 2 Strings variables) that helps me do this.
My problem: I want to have an Add button that takes me to another XML-Layout/class and IDEALLY it input a single item and so when the user goes back to MainActivity the GridView would be updated along with the previous information that I currently hard-coded atm. This previous sentence doesn't work currently
Custom ArrayAdapter and 'WordFolder' is my custom String object that has 2 getters
//constructor - it takes the context and the list of words
WordAdapter(Context context, ArrayList<WordFolder> word){
super(context, 0, word);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view, parent, false);
}
//Getting the current word
WordFolder currentWord = getItem(position);
//making the 2 text view to match our word_folder.xml
TextView title = (TextView) listItemView.findViewById(R.id.title);
title.setText(currentWord.getTitle());
TextView desc = (TextView) listItemView.findViewById(R.id.desc);
desc.setText(currentWord.getTitleDesc());
return listItemView;
}
}
Here is my NewFolder code. Which sets contentview to a different XML. it's pretty empty since I'm lost on what to do
public class NewFolder extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_folder_view);
Button add = (Button) findViewById(R.id.add);
//If the user clicks the add button - it will save the contents to the Word Class
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//make TextView variables and cast the contents to a string and save it to a String variable
TextView name = (TextView) findViewById(R.id.new_folder);
String title = (String) name.getText();
TextView descText = (TextView) findViewById(R.id.desc);
String desc = (String) descText.getText();
//Save it to the Word class
ArrayList<WordFolder> word = new ArrayList<>();
word.add(new WordFolder(title, desc));
//goes back to the MainActivity
Intent intent = new Intent(NewFolder.this, MainActivity.class);
startActivity(intent);
}
});
}
In my WordFolder class I made some TextView variables and save the strings to my ArrayList<> object but so far it's been useless since it doesn't interact with the previous ArrayList<> in ActivityMain which makes sense because its an entirely new object. I thought about making the ArrayList a global variable which atm it doesn't make sense to me and I'm currently lost.
Sample code would be appreciative but looking for a sense of direction on what to do next. I can provide other code if necessary. Thank you
To pass data between Activities to need to do a few things:
First, when the user presses your "Add" button, you want to start the second activity in a way that allows it to return a result. this means, that instead of using startActivity you need to use startActivityForResult.
This method takes an intent and an int.
Use the same intent you used in startActivity.
The int should be a code that helps you identify where a result came from, when a result comes. For this, define some constant in your ActivityMain class:
private static final int ADD_RESULT_CODE = 123;
Now, your button's click listener should looks something like this:
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this, NewFolder.class);
startActivityForResult(intent, ADD_RESULT_CODE);
}
});
Now for returning the result.
First, you shouldn't go back to your main activity by starting another intent.
Instead, you should use finish() (which is a method defined in AppCompatActivity, you can use to finish your activity), this will return the user to the last place he was before this activity - ActivityMain.
And to return some data, too, you can use this code:
Intent intent=new Intent();
intent.putExtra("title",title);
intent.putExtra("desc",desc);
setResult(Activity.RESULT_OK, intent);
where title and desc are the variables you want to pass.
in your case it should look something like this:
public class NewFolder extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_folder_view);
Button add = (Button) findViewById(R.id.add);
//If the user clicks the add button - it will save the contents to the Word Class
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//make TextView variables and cast the contents to a string and save it to a String variable
TextView name = (TextView) findViewById(R.id.new_folder);
String title = (String) name.getText();
TextView descText = (TextView) findViewById(R.id.desc);
String desc = (String) descText.getText();
//Save it to the Word class
ArrayList<WordFolder> word = new ArrayList<>();
word.add(new WordFolder(title, desc));
Intent intent=new Intent();
intent.putExtra("title",title);
intent.putExtra("desc",desc);
setResult(Activity.RESULT_OK, intent);
//goes back to the MainActivity
finish();
}
});
}
You should probably also take care of the case where the user changed his mind and wants to cancel adding an item. in this case you should:
setResult(Activity.RESULT_CANCELLED);
finish();
In your ActivityMain you will have the result code, and if its Activity.RESULT_OK you'll know you should add a new item, but if its Activity.RESULT_CANCELLED you'll know that the user changed their mind
Now all that's left is receiving the data in ActivityMain, and doing whatever you want to do with it (like adding it to the grid view).
To do this you need to override a method called onActivityResult inside ActivityMain:
// Call Back method to get the Message form other Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check the result code to know where the result came from
//and check that the result code is OK
if(resultCode == Activity.RESULT_OK && requestCode == ADD_RESULT_CODE )
{
String title = data.getStringExtra("title");
String desc = data.getStringExtra("desc");
//... now, do whatever you want with these variables in ActivityMain.
}
}
I am currently working on a movie review app. I am trying to pass data from a Recyclerview to another recyclerview in a new activity. Currently I know how to pass data from Recyclerview to textview and imageview. However I looked online and also tried codes but still I could not pass data from recyclerview to recyclerview. What I am trying to pass is a String ArrayList that contains urls. How could I pass the data? Please help me TY! I just recently started on android studio
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Glide.with(mContext)
.asBitmap()
.load(mImageUrls.get(position))
.into(holder.image);
holder.name.setText(mNames.get(position));
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: Clicked on an image: " + mNames.get(position));
Toast.makeText(mContext, mNames.get(position), Toast.LENGTH_SHORT).show();
//Intent intent = new Intent(v.getContext(), MovieDetails.class);
//v.getContext().startActivity(intent);
Intent intent = new Intent(mContext, MovieDetails.class);
intent.putExtra("image_url", mImageUrls.get(position));
intent.putExtra("image_name", mNames.get(position));
intent.putExtra("director_name", mDirector.get(position));
intent.putStringArrayListExtra("movieTrailer", mTrailer);
mContext.startActivity(intent);
}
});
}
private void setImage(String imageUrl, String imageName, String directorName, ArrayList trailerUrl) {
Log.d(TAG, "setImage: setting the image and name to widgets.");
TextView name = findViewById(R.id.text_movie_original_title);
name.setText(imageName);
ImageView image = findViewById(R.id.image_movie_detail_poster);
Glide.with(this)
.asBitmap()
.load(imageUrl)
.into(image);
TextView director = findViewById(R.id.text_movie_director_name);
director.setText(directorName);
RecyclerView trailer = findViewById(R.id.movie_videos);
trailer.set;
bundle.putParcelableArrayList("urlList", list)
send it via intent
get arraylist by
getParcelableArrayList("urlList")
i think these steps would help:
1. create arrays
put the same values to the arrays, you put on the recyclerView.
pass the arrays using intent. Example:
Bundle bundle = new Bundle();
bundle.putStringArray("arrayOfName", new String[]{name1, name2});
bundle.putStringArray("arrayOfImageUrls", new String[]{url1, url2});
Intent intent=new Intent(context, SecondActivity.Class);
intent.putExtras(bundle);
then in the second activity:
Bundle bundle = this.getIntent().getExtras();
String[] names=bundle.getStringArray("arrayOfName");
String[] urls=bundle.getStringArray("arrayOfImageUrls");
add this arrays to the recyclerView adapter in the SecondActivity
set the Adapter to your recyclerView
I will simplify my code to address the problem specifically:
I have an activity A with some TextViews, which text is set to certain key values stored in SharedPreferences, in the activity's OnCreate method. Each textview has a button besides it. When a button is clicked it opens a new activity B which displays an adapter with different text strings. When the user clicks one, the new string is stored in preferences and the user is directed back to Activity A through an intent, and so OnCreate method is called and the textview is updated with the selected text. This works perfectly.
However, my problem is:
When a user does this and updates the textview, if they press Back button once, it will take them to Activity B, but if pressed twice that will take them to Activity A before updating the TextView and thus displaying the old textview, despite having stored in SharedPreferences the updated value. How can this be fixed?
A more simplified version of my problem is, I have a TextView in my layout, and a button which if pressed, deletes it and refreshes the Activity. User presses the delete button, text view disappears, but then presses back button and TextView is restored. That's what I dont want.
I have researched all the back button methodologies and savedInstanceState documentation but I still havent found something that works. I also tried adding an UpNavigation button in my action bar but it does the same effect than the back button.
ACTIVITY A (All these bits of code are called in OnCreate)
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String sound1name = prefs.getString("sound1", "1");
TextView sound1TV = findViewById(R.id.sound1);
sound1TV.setText(sound1Name);
ImageView sound1btn = findViewById(R.id.sound1_btn);
sound1btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent1 = new Intent(getApplicationContext(), SoundSelector.class);
startActivity(intent1);
}
});
ACTIVITY B (calls adapter)
AudioFileAdapter aFilesAdapter = new AudioFileAdapter(SoundSelector.this, audioFileList, soundID);
ListView listView = findViewById(R.id.sounds_list);
listView.setAdapter(aFilesAdapter);
ADAPTER IN ACTIVITY B (OnClickListener when text is selected)
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(contextL);
SharedPreferences.Editor editor = settings.edit();
editor.putString("sound1", sound1string);
editor.apply();
Intent intent1 = new Intent(getContext(), SoundEditor.class);
con.startActivity(intent1);
Im not sure if it is the Activity Lifecycle I have to modify, or intents, or something else but if someone could point me in the right direction I would really appreciate it, if you need any more information or code I'll post as soon as possible.
For storing and retrieving shared preferences try the following:
Storing
SharedPreferences preferences = getSharedPreferences("com.appname", Context.MODE_WORLD_WRITEABLE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("sound1", "YOUR STRING HERE");
editor.apply();
Retrieving
SharedPreferences prfs = getSharedPreferences("com.appname", Context.MODE_PRIVATE);
String soundString = prfs.getString("sound1", "");
Your intent looks fine, are you sure you're passing Activity A's name?
For your second scenario, you could store if the text view was deleted in the shared preference, so when the back button is pressed, it won't display it again in the previous activity.
Something like this
if (isDeleted.equals("Yes")) {
textView.setVisibility(View.INVISIBLE);
}
The way Activity B is navigating back to Activity A by restarting the activity in the front and not by onBackPressed() navigation. Besides if the navigation is an important component to update the string value then the recommended method would be to use startActivityForResult() and update the preference and the TextView upon onActivityResult() of Activity A
class ActivityA extends AppCompatActivity {
private static final int activityRequest = 0x22;
public static final String keyPref = "keyToSharedPrefData";
private TextView mTextView;
private boolean isHidden = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_activity);
mTextView = findViewById(R.id.someTextView);
final String textForTextView = SharedPrefUtils.getString(keyPref);
mTextView.setText(textForTextView);
final Button button = findViewById(R.id.someButton);
if (button != null) {
button.setOnClickListener((view) -> {
final Intent intent = new Intent(ActivityA.this, AcitivtyB.class);
startActivityForResult(intent, activityRequest);
});
}
final deleteButton = findViewById(R.id.delete_button);
if (deleteButton != null) {
deleteButton.setOnClickListener((view) -> {
mTextView.setText("");
isHidden = true;
});
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == activityRequest && resultCode == ActivityB.resultSuccess && data != null) {
if (data.containsKey(ActivityB.resultKey)) {
SharedPrefUtils.saveString(keyPref,
data.getString(ActivityB.resultKey, SharedPrefUtils.getString(keyPref));
if (mTextView != null) {
mTextView.setText(SharedPrefUtils.getString(keyPref));
}
}
}
if (isHidden) {
mTextView.setVisibility(View.GONE);
}
}
}
in ActivityB you can
class ActivityB extends AppCompatActivity {
public static final int resultSuccess = 0x11
public static final int resultFailure = 0x33
public static final String resultKey = "keyForResult"
private void onListItemClick(final String soundString) {
// optional you can also do this
SharedPrefUtils.saveString(ActivityA.keyPref, soundString);
// better to do this
final Intent returnIntent = getIntent() != null ? getIntent() : new Intent();
returnIntent.putExtra(resultKey, soundString);
if (getCallingActivity() != null) {
setResult(returnIntent, resultSuccess);
}
onBackPressed();
}
}
I have an image which is coming from custom Gallery inside fragment's onActivityresult
Calling custom Gallery
Custom_Gallery custom_gallery=new Custom_Gallery();
custom_gallery.setTargetFragment(this,301);
getFragmentManager().beginTransaction().replace(R.id.FragmentMain,custom_gallery).addToBackStack(null).commit();
Sending Result back to fragment from Custom Gallery
bundle.putStringArrayList("Media",media);
Intent intent = new Intent();
intent.putExtras(bundle);
getTargetFragment().onActivityResult(getTargetRequestCode(),getActivity().RESULT_OK,intent);
FragmentManager fragmentManager=getFragmentManager();
fragmentManager.popBackStack();
Getting result inside onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==301 && resultCode==getActivity().RESULT_OK){
List<String> image=data.getStringArrayListExtra("Media");
Log.d(TAG,"Image "+image.get(0));
String img=image.get(0);
//File uploadedImage = new File(image.get(0));
/*Bitmap myImage=BitmapFactory.decodeFile(image.get(0));
uploadImage.setImageBitmap(myImage);*/
Glide.with(getActivity()).load(img)
.apply(new RequestOptions()
.skipMemoryCache(true)
.error(R.drawable.add_fav)
.dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.NONE))
.into(uploadImage);
uploadButton.setEnabled(true);
functions=new App_Functions(getActivity());
Bitmap bitmap = BitmapFactory.decodeFile(image.get(0));
String Base64Image=functions.Convert_To_Base64(bitmap);
//Log.d(TAG,"Base 64 "+Base64Image);
}
}
Here's the image path which I am getting from custom gallery inside Fragment's onActivityResult
/storage/emulated/0/Download/Media/Images/Ximg1520693707996.jpg
I tried with Bitmap's decodefile method then set manually to Imageview but still not working i thought its Glide issue but its not even setting with Bitmap.
Imageview has onClicklistener and i wanted to set selected image to ImageView
There is an issue with lifecycle of android ecosystem. As onResume calls after onActivityResult. All i need to save image path in String when getting result at onActivityResult then use it on set path in ImageView at onResume
I want to add pictures to my favorite activity when a user tap on a picture. So far I'm able to get the data and display it but for some reason whenever I tap on an image it displays the favorited image, however, when I recheck the favorite activity by clicking on it, it shows empty.
Here's the little flow chart.
imageOnTap is implemented on RecyclerAdapter class. I have my Favorite activity and MainActivity.Any help would be appreciated. Thanks
Here's my MyRecyclerAdapter class
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.nameTxt.setText(albums.get(position).getName());
holder.img.setImageResource(albums.get(position).getImage());
//listener
holder.setItemClickListener(new ItemClickListener() {
#Override
public void onItemClick(View v, int pos) {
Toast.makeText(c,albums.get(pos).getName() + " ,added to favorite ",Toast.LENGTH_SHORT).show();
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME,0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("favorite",albums.get(pos).getImage());
editor.commit();
Toast.makeText(c,albums.get(pos).getName() + " ,added to favorite ",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(c, favorite.class);
// intent.putExtra(Intent.EXTRA_TEXT, albums.get(pos).getImage());
c.startActivity(intent);
}
});
}
Here's my favorite activity
public class favorite extends AppCompatActivity {
int favImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite);
ImageView displayImage = (ImageView) findViewById(R.id.movieImage);
SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);
displayImage.setImageResource(settings.getInt("Favorite", 0));
// Intent intent = getIntent();
// if (intent.hasExtra(Intent.EXTRA_TEXT)) {
// favImage = intent.getIntExtra(Intent.EXTRA_TEXT,image);
// displayImage.setImageResource(favImage);
//
// }
}
}
Here's my MainActivity
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_favorite) {
Intent intent = new Intent(this,favorite.class);
startActivity(intent);
}
If you are checking favourite activity from nav menu then it will not display anything afterall you are not passing any intent extras in it. Is it being display when you click the image? Are you getting intent params null here?
Use those preferences:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx.getApplicationContext());
In your case it might be different activities observe different areas of settings.
THAT IS:
use:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx.getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putInt("favorite",albums.get(pos).getImage());
editor.commit();
instead of:
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME,0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("favorite",albums.get(pos).getImage());
editor.commit();
AND
this:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx.getApplicationContext());
displayImage.setImageResource(settings.getInt("Favorite", 0));
instead of:
SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);
displayImage.setImageResource(settings.getInt("Favorite", 0));