Loading images from firestore in image slider - java

I'm trying to load the images in an image slider from firestore. The image urls are there in firestore. I'm using the following library https://github.com/denzcoskun/ImageSlideshow for image slider, but it shows error instead of images.
Image of the output
Logcat Image
Image of firestore structure
This is the java code:
public class preview_product extends AppCompatActivity
{
private String position;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private ImageSlider imageSlider;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_product_preview);
imageSlider = findViewById(R.id.image_slider);
final List<SlideModel> displayImages = new ArrayList<>();
Bundle intent = getIntent().getExtras();
position = intent.get("key").toString();
db.collection("Store").document("name").collection("prod").document(position).collection("details").document("product_id").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>()
{
#Override
public void onSuccess(#NonNull DocumentSnapshot documentSnapshot)
{
List<String> Urls = (List<String>) documentSnapshot.get("Images");
displayImages.add(new SlideModel(Urls.toString(), ScaleTypes.FIT));
imageSlider.setImageList(displayImages,ScaleTypes.FIT);
imageSlider.startSliding(3000);
}
});
}
}

I guess you've copied the link of the webpage-showing the image ,instead of the link of actual image.
Try opening the image in a new tab, and then copy the link from the address-bar.
Maybe, this is what you've copied !
This is what you should try coping instead !
Hope this helps!

Related

App crashes when trying to create a list of files from a directory

[I'm new here]
Hi, I'm trying to create a voice recorder app. I've already figured out how to record audio and play audio. Right now, I just want my app to display a list of all the files contained in a specific directory (for instance, "/Documents/MyApp"). So I can see the file I've just created. For now, I'm just trying to list any file coming from the external storage. The problem is, the app crashes on startup. When I remove the lines about reading external storage, it works perfectly.
I've tried a lot of differents methods online, all pretty similar. But they all leads to the same result : The app crashes on startup. Here's the code:
public class FileAndDirectoryActivity extends AppCompatActivity {
private ArrayList<String> mNames = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_and_directory);
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
File[] arrayFiles = directory.listFiles();
for (File file : arrayFiles){
mNames.add(file.getName());
}
initRecyclerView();
}
private void initRecyclerView() {
RecyclerView recyclerView = findViewById(R.id.recyclerview);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(mNames, this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
I expect the app to list, with the recycler view, the names of all the files contained in a directory. For that, I've created an arrayList called "mNames" getting all the names of the files before getting sent to my RecyclerViewAdapter. But the app crashes on startup...
Your app is probably crashing because it can't complete onCreate (which MUST complete per the Activity lifecycle) because you are not requesting permissions to view the storage.
You need to surround your code with a check that sees if the user has permission to user external storage. You also need to handle cases where the user does not grant permission. More documentation is here. Your code should be modified to look like this:
public class FileAndDirectoryActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST = 1000;
private ArrayList<String> mNames = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_and_directory);
//Check if we have the permission to read storage
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//We dont have the permission, so request it.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST);
}
//We already have permission
else{
permissionExists();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PERMISSION_REQUEST){
if(resultCode == RESULT_OK){
permissionExists();
}
else{
//handle error
}
}
}
private void initRecyclerView() {
RecyclerView recyclerView = findViewById(R.id.recyclerview);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(mNames, this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
private void permissionExists(){
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
File[] arrayFiles = directory.listFiles();
for (File file : arrayFiles) {
mNames.add(file.getName());
}
initRecyclerView();
}
}
Change your constructor like this ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_and_directory);
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
File[] arrayFiles = directory.listFiles();
for (File file : arrayFiles){
mNames.add(file.getName());
}
mNames = arrayFiles== null
? new ArrayList<File>()
: new ArrayList<File>(Arrays.asList(arrayFiles));
initRecyclerView();
}
I'm not sure about that it is going to solve your problem. Because we need to see logcat firstly. If your permissions are ok to see files, you need to initialize your ArrayList like this. I think this is your main problem. Hope this helps

How to use MediaMetadataRetriever SetDataSource for file in external storage

I'm trying to extract Album art from MP3 file But I not able to give proper location of song, which I can give it to SetDataSource method. I tried everything but still it gives me error IllegalArgumentException.
It all happens when this line metaRetriver.setDataSource(MainActivity.this, Uri.parse("/sdcard/song.mp3")); gets executed
My Code.
public class MainActivity extends AppCompatActivity {
MediaMetadataRetriever metaRetriver;
byte[] art;
ImageView album_art;
Bitmap songImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInit();
metaRetriver = new MediaMetadataRetriever();
metaRetriver.setDataSource(MainActivity.this, Uri.parse("/sdcard/song.mp3"));
art = metaRetriver.getEmbeddedPicture();
if (art != null) {
songImage = BitmapFactory
.decodeByteArray(art, 0, art.length);
album_art.setImageBitmap(songImage);
} else {
String error = "art is null";
Log.i("lolol", error);
}
}
public void getInit() {
album_art = (ImageView) findViewById(R.id.album_art);
}
}
Can anyone solve this problem.
From the Android Documentation:
Throws IllegalArgumentException if the Uri is invalid
Double check that the song.mp3 file is in the right place, and the app has permission to see it.

Get data from firebase DB into string inside my program Android studio

I am trying to retrieve data from firebase DB and save data to a string inside my program. All I come across is that I can only view data in a list but find no good guide how to get data without showing all data for the user first. I want it to be run in the background without the user having to see when data is retrieved. In the image below I show what data I want to store inside a string and then use that string to do what I want it to do.
This is the data I want to store inside my string
You can see the blue rings. The data inside the blue rings is what I want to store inside a string or in a string array.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
firebaseAuth = firebaseAuth.getInstance();
listan = (ListView) findViewById(R.id.listView);
DatabaseReference datarefere = FirebaseDatabase.getInstance().getReferenceFromUrl(My URL to database");
FirebaseListAdapter<String> firebaselist = new FirebaseListAdapter<String>(
this,
String.class,
android.R.layout.simple_expandable_list_item_1,
datarefere
) {
#Override
protected void populateView(View v, String model, int position) {
TextView textview = (TextView) v.findViewById(android.R.id.text1);
textview.setText(model);
}
};
listan.setAdapter(firebaselist);
listan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView listan = (TextView) view;
String plats = listan.getText().toString();
}
});
toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
setSupportActionBar(toolbar);
}
In this case i did use listview. The user click on an item inside the list and I save the data to a string. But i dont want to do like that all the time. I want to save data into a string in the background. The user dont need to know or see that it happens.
Maybe something that can help you is this google developer link about how to setup Firebase and other DBs. I hope this helps.

W/Bundle: Key img expected Integer but value was a java.lang.String. The default value 0 was returned

This is adapter.java
#Override
public void onClick(View view) {
int position = getAdapterPosition();
Data data = this.arrayList.get(position);
Intent put_information = new Intent(this.ctx,Display_Gallery_item.class);
put_information.putExtra("title_gallery" ,data.getTitle());
put_information.putExtra("img" , data.getId());
this.ctx.startActivity(put_information);
}
Display_Gallery_item.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_each_item_gallery);
iv_image = (ImageView)findViewById(R.id.iv_gallery_item_show);
tv_title = (TextView)findViewById(R.id.tv_show_each_item_gallery_Title);
iv_image.setImageResource(getIntent().getIntExtra("img",00));
tv_title.setText(getIntent().getStringExtra("title_gallery"));
}
i cant get the title and pass to activity and show . but cant show image in imageview on Display_Gallery_item.java Please Help!!!!!!
This could be a cache conflict issue.
First, Clean Project from Android Studio.
Then, uninstall the app on your device, press Run from Android Studio.
The above steps helped me.

load image into imageView from URL path in DB Table

I'm trying to follow this this tutorial to add image into imageView from URL.
public class AndroidLoadImageFromURLActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.image);
String image_url = "http://api.androidhive.info/images/sample.jpg";
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(image_url, image);
}
}
However, I have the image urls stored in my database table and would like to grab and display dynamically in java. please kindly show me how to do this in java? Thank you so much!
try this link
here there is a function named showImage() which accepts a url as parameter and returns bitmap which you can use to set to the image view.
and dont forget to give the internet permission when dealing with images to be displayed from a url

Categories

Resources