- Home /
how to save compressed image to gallery on Android ?
Hello, this code is working great with rgb color images, how can i make this work with compressed images?
private const string MediaStoreImagesMediaClass = "android.provider.MediaStore$Images$Media";
public static string SaveImageToGallery (Texture2D texture2D, string title, string description)
{
using (var mediaClass = new AndroidJavaClass (MediaStoreImagesMediaClass))
{
using (var cr = Activity.Call<AndroidJavaObject> ("getContentResolver"))
{
var image = Texture2DToAndroidBitmap (texture2D);
var imageUrl = mediaClass.CallStatic<string> ("insertImage", cr, image, title, description);
return imageUrl;
}
}
}
public static AndroidJavaObject Texture2DToAndroidBitmap (Texture2D texture2D)
{
byte[] encoded = texture2D.EncodeToJPG ();
using (var bf = new AndroidJavaClass ("android.graphics.BitmapFactory"))
{
return bf.CallStatic<AndroidJavaObject> ("decodeByteArray", encoded, 0, encoded.Length);
}
}
private static AndroidJavaObject _activity;
public static AndroidJavaObject Activity {
get {
if (_activity == null)
{
var unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
_activity = unityPlayer.GetStatic<AndroidJavaObject> ("currentActivity");
}
return _activity;
}
}
Comment
hi , can you show me your " create image function " i try to create and save image using this method , but still not working . thank you
Just call "SaveImageToGallery" function. You must add write_external_storage permisssion to manifest file.
Answer by Karmate · Oct 10, 2017 at 09:35 AM
still looking for answer, can't find a good solution for this problem.
Your answer
Follow this Question
Related Questions
Download Image to a new album (Android) 0 Answers
[Edited]Access photos from gallery in application (Android) 1 Answer
Save File for Android Game.. 2 Answers
Save a Texture2D for android 1 Answer
pick image from android gallery 3 Answers