Muy buenas. Tengo un código C#/Unity que funciona muy bien para copiar una imagen a la galeria de Android:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// http://answers.unity3d.com/questions/204372/saving-screenshots-to-android-gallery.html
public class AndroidGallery {
private static AndroidJavaObject _activity;
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.EncodeToPNG();
using (var bf = new AndroidJavaClass("android.graphics.BitmapFactory"))
{
return bf.CallStatic<AndroidJavaObject>("decodeByteArray", encoded, 0, encoded.Length);
}
}
public static AndroidJavaObject Activity
{
get
{
if (_activity == null)
{
var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
_activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
}
return _activity;
}
}
}
Ahora necesito mejorarlo para que tambien copie un video mp4. Encontre este codigo en Java que un gran parecido:
// https://stackoverflow.com/questions/2114168/android-mediastore-insertvideo
// Save the name and description of a video in a ContentValues map.
ContentValues values = new ContentValues(2);
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
// values.put(MediaStore.Video.Media.DATA, f.getAbsolutePath());
// Add a new record (identified by uri) without the video, but with the values just set.
Uri uri = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
// Now get a handle to the file for that record, and save the data into it.
try {
InputStream is = new FileInputStream(f);
OutputStream os = getContentResolver().openOutputStream(uri);
byte[] buffer = new byte[4096]; // tweaking this number may increase performance
int len;
while ((len = is.read(buffer)) != -1){
os.write(buffer, 0, len);
}
os.flush();
is.close();
os.close();
} catch (Exception e) {
Log.e(TAG, "exception while writing video: ", e);
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
Alguien me puede dar una mano para convertirlo a Unity?? Voy a intentarlo por mi lado, pero se me hace complicado.