How to load GIF file from gallery || Android studio - android

How to load GIF file from gallery. As it can be lay from accessing the drawable or using the web url. But how to access it from gallery. not like the path i.e. file//folder1//images// file.gif
istead i need to load it via onclick> open gallery(show all files i.e. images, gif etc)> choose one gif and then it open into my app(GifImageView)

In your build.gradle file:
Compile this dependency.
dependencies {
compile 'com.felipecsl:gifimageview:2.1.0'
}
In your Activity class:
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gifView = (GifImageView) findViewById(R.id.gifImageView);
gifView.setBytes(bitmapData);
}
#Override
protected void onStart() {
super.onStart();
gifView.startAnimation();
}
#Override
protected void onStop() {
super.onStop();
gifView.stopAnimation();
}
If you need to post-process the GIF frames, you can do that via GifImageView.setOnFrameAvailable() . You can see an example of that in the sample app included on the repository.
gifImageView.setOnFrameAvailable(new GifImageView.OnFrameAvailable() {
#Override
public Bitmap onFrameAvailable(Bitmap bitmap) {
return blurFilter.blur(bitmap);
}
});
You can also reset an animation to play again from the beginning gifImageView.resetAnimation(); or show a specific frame of the animation gifImageView.gotoFrame(3);

Related

Image not found /Documents/Image:XXXX

I'm selecting an image from the gallery selector after pressing a button, and then filling a RecyclerView with these images. However, it seems the pictures I'm selecting cannot be found...
To open the gallery selector I do:
ActivityResultLauncher<String> GetImageFromGallery = registerForActivityResult(new ActivityResultContracts.GetContent(),
new ActivityResultCallback<Uri>() {
#Override
public void onActivityResult(#Nullable Uri uri) {
if (uri != null) {
ArrayList<String> temp = new ArrayList<>(0);
temp.add(uri.getPath());
img_data.add(temp);
mAdapter.notifyDataSetChanged();
}
}
});
// Button
Button btn_add = root.findViewById(R.id.btn_add_image);
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
GetImageFromGallery.launch("image/*");
}
});
where img_data is an `ArrayList<ArrayList> and mAdapter is the adapter of a RecyclerView. Inside the RecyclerView I'm reading the images to show in an ImageViewer as follows:
try {
viewHolder.getphoto().setImageBitmap(BitmapFactory.decodeStream(viewHolder.getphoto().getContext().getApplicationContext().getContentResolver().openInputStream(Uri.parse(localDataSet.get(position).get(0)))));
//Or viewHolder.getphoto().setImageBitmap(BitmapFactory.decodeFile(localDataSet.get(position).get(0)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
However, none of above works. I'm getting stuck with a not found file error. Path are always of style /Documents/Image:XXXX where X are numbers. What am I doing wrong?
Following the code in my question, use this to add the uri as a String:
temp.add(uri.toString());
and this to load the image:
viewHolder.getphoto().setImageURI(Uri.parse(localDataSet.get(position).get(0)));
Although if possible use Glide to load the image. Without Glide was extremely slow, but with it it wasn't. To add glide add the following dependencies intro build.gradle module:
implementation 'com.github.bumptech.glide:glide:4.12.0'
// Glide v4 uses this new annotation processor -- see https://bumptech.github.io/glide/doc/generatedapi.html
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
and then to load the picture:
Glide.with(viewHolder.getphoto().getContext())
.load(Uri.parse(localDataSet.get(position).get(0)))
.into(viewHolder.getphoto());

Is there a better way to load a picture in android studio?

I am making an app using Java in android studio, and in it I want users to have the option to set a custom profile picture. I'm using firebase storage to store the images for the PFPs, and taking them from there every time I load it, but it always takes around half a second or so to load the profile picture: gif of the problem
Here's my code for loading the pfp in the homepage (I am using de.hdodenhof.circleimageview.CircleImageView to make the ImageView circular):
public class HomePage extends AppCompatActivity {
de.hdodenhof.circleimageview.CircleImageView pfpImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
pfpImage = findViewById(R.id.pfpImg);
Intent intent = getIntent();
if (intent != null && intent.hasExtra("UserEmail")) {
String userEmail = intent.getExtras().getString("UserEmail");
StorageReference userPfp = DBRef.refStorage.child("ProfileImages/Users/" + userEmail.replace(".",",")); //DBRef is a class I created to reference all Firebase related objects.
userPfp.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Picasso.with(HomePage.this).load(uri).into(pfpImage); // I am using com.squareup.picasso:picasso:2.5.2 to load the image from the Uri.
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(HomePage.this, "Profile Picture Loading Failed", Toast.LENGTH_SHORT).show();
}
});
}
else
pfpImage.setImageResource(R.drawable.doggo);
}
}
Is there some better way to load/store the pictures so they appear when the page loads?
EDIT: I managed to negate this by not loading from Firebase every time the activity is opened, but only the first time and saving it, using an idea from this post
There is other library named glide that can load picture faster when the page loads.
Here is the link for the reference.
https://guides.codepath.com/android/Displaying-Images-with-the-Glide-Library

Image are not displaying from Cache memory using Picasso Library

I am using Picasso library for image download and displaying it in imageView. This library also store images in cache and memory. When my internet is turn on, i am able to view images on imageView. So i think, it should also be store in cache or file memory. Now my internet is turnOFF, but it doest not display to images. kindly have a look.
Picasso.with(context)
.load(url) .placeholder(R.drawable.defaultimg)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.imageview2, new ImageLoadedCallback(holder.loadingBar) {
#Override
public void onSuccess() {
if (holder.loadingBar != null) {
holder.loadingBar.setVisibility(View.GONE);
}
}
#Override
public void onError(){
holder.loadingBar.setVisibility(View.VISIBLE);
Picasso.with(context)
.load(url) .placeholder(R.drawable.defaultimg)
.into(holder.imageview2, new ImageLoadedCallback(holder.loadingBar) {
#Override
public void onSuccess() {
if (holder.loadingBar != null) {
holder.loadingBar.setVisibility(View.GONE);
}
}
#Override
public void onError() {
if (holder.loadingBar != null) {
holder.loadingBar.setVisibility(View.GONE);
}
}
});
}
});
Finally I resolved that issue. Thanks #dev.bmax
image url was not correct. There is bug in Picasso. If we have url like as
https://i.ytimg.com/vi/DMVEcfQmPOs/maxresdefault.jpg?500|700
Picasso is able to displaying image when internet TURN ON
but if we TURN OFF to internet, it does not decode the url. and not display the image also.
We have to remove ?500|700 then i was able to view image in OFFLine mode also.
//url.substring(0,url.indexOf("?"))
https://i.ytimg.com/vi/DMVEcfQmPOs/maxresdefault.jpg
Thanks!

QR code reader to extract data

i have a sales summary print out and has a QR Code ,
i want to develop an app (IOS and android) that reads the QR code , extract all information,do some calculations,and display in specific form , i tried zxing library but it did not extract all information from the receipt.any tip?
You can use google vision API to achieve this. I personally used this and found it great. The below code snippets should help you.
Put this below line in the gradle.
compile 'com.google.android.gms:play-services:9.4.0'
Use BarcodeDetector and CameraSource classes to capture the QR code on real time and decode it.
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
#Override
public void release() {
}
#Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0) {
barcodeInfo.post(new Runnable() { // Use the post method of the TextView
public void run() {
barcodeInfo.setText( // Update the TextView
barcodes.valueAt(0).displayValue
);
}
});
}
}
});
Use a SparseArray to fetch the detections and the displayValue of the elements of this sparse array returns the deocded string.
After extracting the string one can do anything, be it displaying the string or make some calculation out of it etc.
This library is the most popular and easiest of reading QR codes in your Android application.
You should also have a look at the Wiki section of this library for learning about how to integrate this library into your Android Application and how to use this library.
This is how you can use this library.
1. Add this library to your project by adding following line into your dependencies inside build.gradle(Module: app) file
compile 'com.github.nisrulz:qreader:2.0.0'
2. Then, after syncing project files, add the SurfaceView element provided by this library into your XML layout file.
<SurfaceView
android:id="#+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
3. Declare the SurfaceView & QREader inside your Activity's Java file & then initialize it inside onCreate() method.
class MainActivity extends AppCompatActivity{
private SurfaceView mySurfaceView;
private QREader qrEader;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setup SurfaceView
// -----------------
mySurfaceView = (SurfaceView) findViewById(R.id.camera_view);
// Init QREader
// ------------
qrEader = new QREader.Builder(this, mySurfaceView, new QRDataListener() {
#Override
public void onDetected(final String data) {
Log.d("QREader", "Value : " + data);
text.post(new Runnable() {
#Override
public void run() {
text.setText(data);
}
});
}
}).facing(QREader.BACK_CAM)
.enableAutofocus(true)
.height(mySurfaceView.getHeight())
.width(mySurfaceView.getWidth())
.build();
}
4. Initialize it inside onResume()
#Override
protected void onResume() {
super.onResume();
// Init and Start with SurfaceView
// -------------------------------
qrEader.initAndStart(mySurfaceView);
}
There are many more possibilities you can do with this library, so I recommend you to visit the GitHub repository and check it out. It's worth a shot!

Picasso - Download image once then load into multiple ImageView

Does anyone know how to download an image once then use the same Picasso instance to load into multiple ImageView? Right now I'm using a (pretty bad) workaround like the one below, in order to be sure the image is already cached and not downloaded again.
Picasso.with(container.getContext()).load(photo.getPath()).placeholder(R.drawable.placeholder_outfit).fit().centerCrop().into(image1, new Callback() {
#Override
public void onSuccess() {
Picasso.with(container.getContext()).load(photo.getPath()).placeholder(R.drawable.placeholder_outfit).fit().centerCrop().into(image2);
}
#Override
public void onError() {
}
});
You can do it like this:
Picasso.with(container.getContext())
.load(photo.getPath())
.placeholder(R.drawable.placeholder_outfit)
.fit()
.centerCrop().into(image1, new Callback() {
#Override
public void onSuccess() {
imageView2.setImageDrawable(image1.getDrawable()); //Get the ImageView's image (this won't download it, it will get the downloaded image) and set it to your second imageView.
}
#Override
public void onError() {
}
});
To use centerCrop(), just add:
imageView2.setScaleType(ImageView.ScaleType.CENTER_CROP);

Categories

Resources