Share file using Glide - android

I'm trying to share GIF file. I want to share it via any social networking apps, I have a problem with file name. I don't how to get name file. This is my code:
llsetwallpapers.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// set as wallpapers
lt.setText("Please Wait ...");
lt.setTranslationY(100);
lt.show();
Glide.with(getApplicationContext())
.load(url)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<?super Bitmap> glideAnimation) {
File file = new File file = new File(getApplicationContext().getExternalCacheDir() , namefile)
file.setReadable(true);
Uri uri = Uri.fromFile(file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/gif");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share with"));
}
});
}
});

The problem you are trying to solve is discussed at github issues, where RĂ³bert Papp has provided an example implementation for sharing a file downloaded by Glide.
The key part for you - is to get a reference to the File. That can be done with downloadOnly():
File file = Glide
.with(context)
.load(url)
.downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get()
Having a File, you can use FileProvider API in order to share the file.

Related

Show .gif from Firebase Storage link

I have link in my Firebase Storage on .gif file
String gifUrl = "https://firebasestorage.googleapis.com/v0/b/rokitskiydev-a18ca.appspot.com/o/WF%2F10-16-02-Digital-15.gif?alt=media&token=0354f6e6-4292-4911-9302-49281d293a80";
I try use Picasso and Glide. But I can show only image of this gif.
GlideApp.with(context).load(gifUrl).into(ImageView);
If i get another link, an example http://i.imgur.com/1ALnB2s.gif it's OK!
How can I do this?
Get the downloadUrl of the gif and then use that to load the gif with Glide.
ImageView imageView = (ImageView) findViewById(R.id.imageView);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
Glide.with(this).load(downloadUrl).into(imageViewTarget);
For newer versions of Glide try this:
Glide.with(context)
.load(imageUrl)
.asGif()
.placeholder(R.drawable.loading2)
.crossFade()
.into(imageView);
As Glide Documentation says,
You can use .asGif() to force the library to load an animated gif and fail if it is not :
Glide.with(activity).load(gifUrl).asGif().into(view);
try this
Check the doc: https://futurestud.io/tutorials/glide-displaying-gifs-and-videos
also as FirebaseDoc says you can do this to load an image/gif with glide
first declare this in your build.gradle
dependencies {
// FirebaseUI Storage only
compile 'com.firebaseui:firebase-ui-storage:0.6.0'
}
then
this to load your image with firebase
// Reference to an image file in Firebase Storage
StorageReference storageReference = ...;
// ImageView in your Activity
ImageView imageView = ...;
// Load the image using Glide
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(storageReference)
.into(imageView);
if you need the url of your image/gif file, you can do this
storageRef.child("your/firebase/imagepath.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
Uri downloadUri = taskSnapshot.getMetadata().getDownloadUrl();
generatedFilePath = downloadUri.toString(); /// The string(file link) that you need
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});

Android how to Share GIF image to all Apps without converting it to in jpg or png format

Can any body tell me how can i share GIF image in android programmatically like.
Actually i want to share GIF image like android share .jpg or .png images.
I search a lot of but there is no link to share GIF image,please if anybody know please tell me.
code for sharing GIF image? startActivity(Intent.createChooser(shareIntent,"Share Emoji")); is used to share data using intent with the title "Share Emoji" whenever we share jpg or png it works perfectly. But if we try to send GIF images it does not work perfectly only email way works, if we select any other app to share GIF image like WhatsApp or Shareit, it converts GIF image to simple png image..... By this code only, i am unable to share GIF image with every other apps. So please check it and give me reasonable & practical solution if this is possible
Thank you
===============================================================
this is my code which i used currently
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_main);
Button click = (Button) findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
shareGif(getResources().getResourceName(R.drawable.clark));
}
});
}
private void shareGif(String resourceName) {
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "sharingGif.gif";
File sharingGifFile = new File(baseDir, fileName);
try {
byte[] readData = new byte[1024 * 500];
InputStream fis = getResources().openRawResource(getResources().getIdentifier(resourceName, "drawable", getPackageName()));
FileOutputStream fos = new FileOutputStream(sharingGifFile);
int i = fis.read(readData);
while (i != -1) {
fos.write(readData, 0, i);
i = fis.read(readData);
}
fos.close();
} catch (IOException io) {
}
System.out.println("===sharing file=="+sharingGifFile);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/gif");
Uri uri = Uri.fromFile(sharingGifFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Emoji"));
}
}

Sharing GIF in Android

I am working on an Android app wherein I have to implement functionality of sharing GIF with other messaging app.
For that I am storing in a temporary file and passing file's url through intent. My gif is stored as a drawable. Thus while retrieving it I used FileOutputStream with Bitmap.Compress. Due to this instead of GIF I am getting a stil image.
Also, I tried converting Bitmap to Byte Array without compressing, but in this case my image is not showing up
My code to share gif:
int drawableId = gridItemList.get(position);
Bitmap contentToShare = BitmapFactory.decodeResource(getResources(), drawableId);
try {
File cachePath=new File(getApplicationContext().getFilesDir(),"content");
cachePath.mkdirs();
FileOutputStream stream=new FileOutputStream(cachePath+"/anim.gif");
contentToShare.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
File imagePath = new File(getApplicationContext().getFilesDir(), "content");
File newFile = new File(imagePath,"anim.gif");
Uri contentURI=FileProvider.getUriForFile(getApplicationContext(),getPackageName()+".fileprovider",newFile);
if(contentURI!=null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, contentURI);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.setType("image/gif");
startActivity(shareIntent);
}
}catch (Exception e) {
Log.e("Exception",e.toString());
}
PS : Sharing jpeg and png is working perfectly.
Use Glide library to show your GIF
In Your Gradle:
compile 'com.github.bumptech.glide:glide:3.7.0'
Code to write:
String gifUrl = "http://i.kinja-img.com/gawker-media/image/upload/s--B7tUiM5l--/gf2r69yorbdesguga10i.gif";
Glide
.with( context )
.load( gifUrl )
.into( imageViewGif );

Problems on sharing bitmap images

I have used com.nostra13.universalimageloader to load image. Inside the standard ImageAdapter in a ViewPager, there is a share button to share the bitmap. The code is as follows:
Code:
#Override
public Object instantiateItem(ViewGroup view, int position)
{
....
#Override
public void onLoadingComplete(String imageUri, View view, final Bitmap loadedImage)
{
spinner.setVisibility(View.GONE);
btn_share = (ImageButton) imageLayout.findViewById(R.id.btn_share);
btn_share.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Shining Card";
File dir = new File(file_path);
if (!dir.exists()) dir.mkdirs();
File file = new File(dir, "to share");
FileOutputStream fOut;
try
{
fOut = new FileOutputStream(file);
loadedImage.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
}
catch (Exception e)
{
e.printStackTrace();
Constants.custom_toast(getActivity(), "Error when sharing", "");
}
Uri uri = Uri.fromFile(file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share Cover Image"));
}
});
}
Question:
The problem lies on sharing the image.
Share via whatsapp
Sharing the first image via whatsapp, the image can be shared successfully to others. Continues with sharing second image it also works successfully.
However, if the sharing of the first image is cancelled in whatsapp and return to the app, and then choose another image to share in whatsapp, the preview in whatsapp will remain as the first image, though if ignore it and continue to share whatsapp will actually share the new image. (i.e. there is a preview problem) (using other app to share image perform the same, preview in whatsapp does not go wrong)
Share via gmail
Sharing the image as attachment via gmail, the image cannot be previewed nor opened.

How to share image that is downloaded and cached with Volley?

In my app I download images from server using Volley and its ImageLoader (with BitmapLruCache). I want to create a share option, researched a little bit, and found that share intent can only share localy stored images. First question, is this right?
Second, if that is right, what should I do? Should I download image again and save it to local storage? Or put it in MediaStore? Or I can pull image from cache and share cached version? What are the best practices?
Any suggestion is welcome, or code snippet.
What I did is made another request for getting the image, and added an ImageListener which provides ImageContainer containing bitmap.
MyApplication.getImageLoader(activity).get(imageURL, new ImageListener() {
#Override
public void onErrorResponse(VolleyError error) {}
#Override
public void onResponse(ImageContainer response, boolean isImmediate) {
Bitmap mBitmap = response.getBitmap();
ContentValues image = new ContentValues();
image.put(Media.MIME_TYPE, "image/jpg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, image);
try {
OutputStream out = getContentResolver().openOutputStream(uri);
boolean success = mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
if (!success) {
} else {
imageUri = uri;
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Create the share Intent
Intent shareIntent = ShareCompat.IntentBuilder.from(activity).setType("image/jpg").setText(shareText).getIntent();
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
mShareActionProvider.setShareIntent(shareIntent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
As image is already downloaded and cached (using BitmapLRU cache and ImageLoader), this new request gives me bitmap from cache so no new network data is made.

Categories

Resources