I'm using the Universal Image Loader library.I have a viewpager gallery of images and I want to be able to save the current image being viewed to the SD-Card. Below is the button i've implemented but it doesn't work. PLEASE HELP!!
private ViewPager gallery;
public class ImageGalleryActivity extends BaseActivity {
final String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
.....
findViewById(R.id.btnSave).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int index = gallery.getCurrentItem();
ImageView img = imageUrls.get(index);
saveimage(img);
Toast.makeText(ImageGalleryActivity.this, "Image Successfully Saved", Toast.LENGTH_LONG).show();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://Pictures"
+ Environment.getExternalStorageDirectory())));
}
});
...
}
Make sure you added the appropriate permission in your app manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Bundle is generally used for passing data between various Activities of android. It depends on you what type of values you want to pass but bundle can hold all types of values and pass to the new activity.
You can use it like this,
Intent intent = new Intent(getApplicationContext(),ActivityName.class);
intent.putExtra("myKey",AnyValue);
startActivity(intent);
Now you can get the passed values by...
Bundle extras = intent.getExtras();
String tmp = extras.getString("myKey");
Have below line inside onCreate method after
public class MyActivity extends Activity
{
private static String imageURL;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageUrls = bundle.getStringArray(Extra.IMAGES);
}}
My best solution is don't pass anything via bundle to another activity.
Instead use this class,
public class MyGetSetMethod()
{
String[] imageURLS;
public void setImageURLS (String[] imageUrls)
{
this.imageURLS = imageUrls;
}
public String[] getImageURLS()
{
return imageURLS;
}
}
Now on your ImagePagerActivity,
MyGetSetMethod.setImageURLS(imageURLS);
On ImageGalleryActivity,
String[] ImageURLS = MyGetSetMethod.getImageUrls();
This should do.
Related
I need help.
I create android recyclerview cardview project, when data list from cardview clicked (use putExtra dan getExtra) will call new activity , but not working (in new activity blank no content).
this my code (CardViewAndroidAdapter.java) my full code error when i paste in here - ask in stackoverflow
holder.btnDetail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("PUT_PHOTO", getListAndroid().get(position).getPhoto());
intent.putExtra("PUT_DESK", getListAndroid().get(position).getDesk());
context.startActivity(intent);
}
});
this my code (DetailActivity.java) my full code error when i paste in here
public class DetailActivity extends AppCompatActivity {
private static final String GET_PHOTO = "get_photo";
private static final String GET_DESK = "get_desk";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
getSupportActionBar().setTitle("Detail Version");
getIncomingIntent();
}
private void getIncomingIntent() {
if(getIntent().hasExtra(GET_PHOTO)&&getIntent().hasExtra(GET_DESK)){
String getPhoto=getIntent().getStringExtra(GET_PHOTO);
String getDesk = getIntent().getStringExtra(GET_DESK);
TextView detail=findViewById(R.id.tv_detail);
detail.setText(getDesk);
ImageView img_item_photo=findViewById(R.id.img_item_photo);
Glide.with(this)
.load(getPhoto)
.into(img_item_photo);
}
}
Replace this:
intent.putExtra("PUT_PHOTO", getListAndroid().get(position).getPhoto());
intent.putExtra("PUT_DESK", getListAndroid().get(position).getDesk());
with this:
intent.putExtra(DetailActivity.GET_PHOTO, getListAndroid().get(position).getPhoto());
intent.putExtra(DetailActivity.GET_DESK, getListAndroid().get(position).getDesk());
It should work now.
I am new to learning Array in android studio. Please show me some examples in details. I have write an example here and I want to display the Array data from MainActivity into second_page activity .
MainActivity.java
public class MainActivity extends AppCompatActivity {
String my_array[]={"dog","cat","tiger"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void next_page(View view){
Intent intent = new Intent(this,second_page.class);
intent.putExtra("my_array_next", my_array);
startActivity(intent);
}
}
second_page.java
public class second_page extends MainActivity {
TextView get_data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_page);
get_data=(TextView)findViewById(R.id.tv);
Intent intent=getIntent();
// coding here to display the array data
// sth like abc.setText(display_array_data);
}
Please advice. Thank you!
If you are trying to send a String-array from one Activity to another this can be done in the Intent.
In ClassA:
Intent intent = new Intent(this, ClassB);
String[] my_array = new String[]{"dog","cat","tiger"};
intent.putExtra("myArr", my_array);
startActivity(intent);
In ClassB:
public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("myArr");
}
this may helps you
In second_page.java, receive the array you pass via Intent and set it to your TextView like this
#Override
protected void onCreate(Bundle savedInstanceState) {
...
String[] array = intent.getStringArrayExtra("my_array_next");
// TextView display a String so you should convert your Array to String
String str1 = Arrays.toString(array);
get_data.setText(str1);
}
First take the array:
Intent intent = getIntent();
List array;
if (intent.getExtras() != null) {
array= intent.getExtras().getBoolean("my_array_next");
}
Then print
get_data.setText(array.toString());
Sending Class:
Intent intent = new Intent(this, ClassB);
String[] myStrings = new String[] {"test", "test2"};
intent.putExtra("strings", myStrings);
startActivity(intent);
Reciving Class:
public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");
}
In your Second activity:
String[] array=getIntent().getStringArrayExtra("my_array_next");
I think you need to go through basics, go to https://developer.android.com/index.html to get started.
So I am making an android app that implements some custom classes. I want to create an object of class Menu_Organizer to other activities, but after I inizialize the object and send it to the next Activity the object is NULL. Here are the classes:
Menu Items class
import java.io.Serializable;
public class Menu_Items implements Serializable {
private String Name = "";
private String Description = "";
private float Price = 0.0f;
private String Category = "";
/* Getters and Setters*/
}
Menu Organizer class:
public class Menu_Organizer implements Serializable {
ArrayList<String> Categories;
ArrayList<Menu_Items> Food;
// EDITED
public Menu_Organizer() {
Categories = new ArrayList<String>();
Food = new ArrayList<Menu_Items>();
}
/* Other class methods */
}
First Activity (main)
public class MainActivity extends AppCompatActivity {
private Button btn;
public Menu_Organizer menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onclick();
}
public void onclick() {
btn = (Button) findViewById(R.id. btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Menu_Organizer menu = new Menu_Organizer();
menu.New_Menu_Item("Carne", "Pollo", "Pollo Asado Rico!", 4.55f);
Intent activity2= new Intent(MainActivity.this,temp.class);
activity2.putExtra("Menu", menu);
startActivity(activity2);
}
});
}
}
Second Activity
public class temp extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Second);
Bundle bundle = getIntent().getExtras();
Menu_Organizer menu = (Menu_Organizer) bundle.getSerializable("Menu");
String str= menu.Food.get(0).getName();
}
}
Alright, i think that the issue is, that when you pass your class object in a key value pair, you do it in an Intent object, but when you resolve your intent, you do that via a bundle object. So, in you temp Activity, you should resolve the intent like:
Intent intent = this.getIntent();
Menu_Organizer menu = (Menu_Organizer) intent.getSerializableExtra("Menu");
Try this, this should work. If you want to do it via a bundle, then create a bundle object first, then put whatever you want in that bundle. Add the bundle to your intent, and then resolve the bundle in your next Activity.
Just a tip, Class names generally do not contain _ in them, use CamelCase naming convention for all classes.
I would recommend using EventBus library for this kind of thing. It is quite easy to use and gives you exactly this: sending and receiving custom object classes from one place to another (Fragments, Activities, Services, whatever you wish can send and receive objects).
I personally don't like intents cause they have too many limitations.
I have been following a tutorial from this link which is basically about File browse concept in android. Everything works fine but I am getting confused of how passing intents between activities works in android after reading this link. The First activity is as follows,
public class MainActivity extends Activity implements OnClickListener {
private static final int REQUEST_PICK_FILE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filePath = (TextView)findViewById(R.id.file_path);
Browse = (Button)findViewById(R.id.browse);
Browse.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.browse:
Intent intent = new Intent(this, FilePicker.class);
startActivityForResult(intent, REQUEST_PICK_FILE);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
switch(requestCode) {
case REQUEST_PICK_FILE:
if(data.hasExtra(FilePicker.EXTRA_FILE_PATH)) {
selectedFile = new File
(data.getStringExtra(FilePicker.EXTRA_FILE_PATH));
filePath.setText(selectedFile.getPath());
}
break;
}
}
As far as I understood, its passing the intent "REQUEST_PICK_FILE" and based on what it returns from "FilePicker.Class" , it will perform the action because its StartActivityOnResult. Confusion starts from next activity. Here is the File Picker class,
public class FilePicker extends ListActivity {
public final static String EXTRA_FILE_PATH = "file_path";
public final static String EXTRA_SHOW_HIDDEN_FILES = "show_hidden_files";
public final static String EXTRA_ACCEPTED_FILE_EXTENSIONS = "accepted_file_extensions";
private final static String DEFAULT_INITIAL_DIRECTORY = "/";
.......
protected String[] acceptedFileExtensions;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.........
// Initialize the extensions array to allow any file extensions
acceptedFileExtensions = new String[] {};
// Get intent extras
if(getIntent().hasExtra(EXTRA_FILE_PATH))
Directory = new File(getIntent().getStringExtra(EXTRA_FILE_PATH));
if(getIntent().hasExtra(EXTRA_SHOW_HIDDEN_FILES))
ShowHiddenFiles = getIntent().getBooleanExtra(EXTRA_SHOW_HIDDEN_FILES, false);
if(getIntent().hasExtra(EXTRA_ACCEPTED_FILE_EXTENSIONS)) {
ArrayList<String> collection =
getIntent().getStringArrayListExtra(EXTRA_ACCEPTED_FILE_EXTENSIONS);
acceptedFileExtensions = (String[])
collection.toArray(new String[collection.size()]);
}
}
..............
Whats actually happening here? what does the lines ,
if(getIntent().hasExtra(EXTRA_SHOW_HIDDEN_FILES))
ShowHiddenFiles = getIntent().getBooleanExtra(EXTRA_SHOW_HIDDEN_FILES, false);
actually mean? We are not passing any extras like, "EXTRA_SHOW_HIDDEN_FILES" from previous activity and even EXTRA_SHOW_HIDDEN_FILES has been declared in this class. I seriously don't understand what's happening between these two activities . I am not able to proceed before understanding what's actually going on. Any help would be really great !! Thanks.
what does the lines... actually mean?
It means that if the Intent used to start FilePicker has an EXTRA_SHOW_HIDDEN_FILES, hold onto that value in what I assume is a boolean field on the activity that is not shown in your redacted code listing.
We are not passing any extras like, "EXTRA_SHOW_HIDDEN_FILES" from previous activity
You could, though. You do not have to.
even EXTRA_SHOW_HIDDEN_FILES has been declared in this class
That is fairly typical. FilePicker is declaring an API, and so it exposes the names to be used for incoming and outgoing extras. EXTRA_SHOW_HIDDEN_FILES is public, and so it can be referenced from anywhere, including MainActivity.
I'm working with an adapter(viewpager) and when the app changes orientation it loses its information. Which is the easiest way to conserve this information? I was going to use savedInstanceState inside the adapter but it doesn't recognise it.
Finally, I have done it following the advice of Jofre as he pointed in Activity restart on rotation Android
and looking in http://developer.android.com/intl/es/guide/topics/resources/runtime-changes.html
YOU MUST NOT MODIFY MANIFEST. DON'T ADD android:configChanges
SOLUTION:
//ALL THE VARIABLES YOU NEED TO SAVE IN YOUR ADAPTER PUT THEM AS PUBLIC, IE:
//public int a = 2;
//public String b = "a";
Public class Yourclass extends Activity{
Classofyouradapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
final Save_object data = (Save_object) getLastNonConfigurationInstance();
adapter = new Classofyouradapter(your variables);
if (data != null) {
fillit(data);
}
myPager = (ViewPager) findViewById(R.id.yourpanelpager);
myPager.setAdapter(adapter);
}
#Override
public Object onRetainNonConfigurationInstance() {
final Save_object data = new Save_object();
return data;
}
public class Save_object{
int a = adapter.a;
String b = adapter.b;
//Rest of variables
}
public void fillit(Save_object data){
adapter.a= data.a;
adapter.b= data.b;
//Rest of variables
}
}