unity上面调用andorid相册的插件-android部分
来源:互联网 发布:阿里云与传统工业合作 编辑:程序博客网 时间:2024/05/16 19:26
由于最近项目需要做,一个简单的unity3d上面链接android原生相册获取图片的插件,于是乎简单的了解了一下,以下是实现步骤:
1.从unity3d的项目路径中拷出jar包unity3D5.0\u3d\Unity\Editor\Data\PlaybackEngines\androidplayer\release\bin\classess
2.新建一个acitivity把classes.jre包放入libs文件夹,并创建一个继承自UnityPlayerActivity的类
2.1导入classes.jar
2.2创建继承自UnityPlayerActivity的类
public class Main extends UnityPlayerActivity{private String photoPath="";public static String FILE_NAME = "image.png";public static final int CHOOSE_PICTURE=4;private String newPath=""; private String headPath="";@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}}3.主要的类中书写供unity脚本回调的方法(Main()是实现打开相册的方法,RefreshPic是实现图片截图之后保存到照相机中的相册方法)
public void Main(){Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT); openAlbumIntent.setType("image/*"); startActivityForResult(openAlbumIntent, CHOOSE_PICTURE); } public void RefreshPic(String oldpath) { photoPath="/mnt/sdcard/DCIM/Camera/"; SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyyMMddhhmmss"); FILE_NAME="p"+sDateFormat.format(new java.util.Date())+".png"; copyFile(oldpath, photoPath); myDeleteFile(oldpath); Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(new File(photoPath+FILE_NAME)); intent.setData(uri); sendBroadcast(intent); }
4.书写打开相册选择图片之后的回调方法
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { // 拍照 if(requestCode==CHOOSE_PICTURE) { Uri originalUri = data.getData(); String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(originalUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); //判断游标是否为空 if(cursor.getString(column_index)!=null) { //得到选择图片的原始路径 String oldpath = cursor.getString(column_index); //保存原始图片的路径 newPath="/mnt/sdcard/Android/data/com.unitypluginstest.main/files/sourcefiles"; //保存压缩后图片的路径 headPath="/mnt/sdcard/Android/data/com.unitypluginstest.main/files/headfiles"; SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyyMMddhhmmss"); FILE_NAME="p"+sDateFormat.format(new java.util.Date())+".png"; //将选择的图片文件复制到新的路径 copyFile(oldpath, newPath); //位图工厂模式打开对图片进行压缩设置 BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize =2; Bitmap b = BitmapFactory.decodeFile(newPath+"/"+FILE_NAME, options); BitmapFactory.Options options1 = new BitmapFactory.Options(); options1.inSampleSize =getSuitableSize(b.getWidth()*2,b.getHeight()*2); Bitmap b1= BitmapFactory.decodeFile(newPath+"/"+FILE_NAME, options1); saveBitmap(headPath, b1); } else { Toast.makeText(getApplicationContext(), "当前的路径不存在", Toast.LENGTH_SHORT).show(); } }super.onActivityResult(requestCode, resultCode, data);}5.书写回调图片方法之后的相关工具方法(文件复制,压缩,保存图片等)
public void copyFile(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); File newfile=new File(newPath); if(!newfile.exists()) { newfile.mkdirs(); } if (oldfile.exists()) { InputStream inStream = new FileInputStream(oldPath); //读入原文件 FileOutputStream fs = new FileOutputStream(newPath+"/"+FILE_NAME); byte[] buffer = new byte[1024]; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; fs.write(buffer, 0, byteread); } Log.e("tag", "success!"); inStream.close(); fs.close(); } } catch (Exception e) { e.printStackTrace(); } } public boolean myDeleteFile(String path) { File file=new File(path); if(file.exists()) { file.delete(); return true; }else { return false; } }private int getSuitableSize(int width, int height) {int suitableSize = 16;if(width>height){suitableSize=Math.round(width/128);}else if(height>width) {suitableSize=Math.round(height/128);}else {suitableSize=Math.round(width/128);}return suitableSize;}public void saveBitmap(String filePath,Bitmap bmp) { File file = new File(filePath); if(!file.exists()){file.mkdirs();}try { FileOutputStream out = new FileOutputStream(file+"/"+FILE_NAME); bmp.compress(Bitmap.CompressFormat.PNG, 30, out); out.flush(); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
6.生成自己编写的jar包,并且导出相关的资源文件
0 0
- unity上面调用andorid相册的插件-android部分
- unity上面调用andorid相册的插件-unity部分
- Unity调用Android保存图片到相册
- Unity调用Android 相机和相册
- Android Java调用.so和.a、Unity C#调用jar插件和C++开发的插件
- 使用Andorid Studio 创建unity 插件
- 调用相同相机,相册(2部分分开,简单的)
- android调用拍照相册遇到的问题
- Android调用系统相册的方法
- unity调用eclipse导出的jar插件
- Android Studio 在Mac平台上面常用的部分快捷键
- Android Studio 在Mac平台上面常用的部分快捷键
- Android调用相机,相册
- 调用系统相册 Android
- Android调用相册
- android调用系统相册
- Unity插件之Unity调用C#编译的DLL
- andorid adapteritem 上面的button点击时跳转页面
- 数据结构:倒排索引
- Linux内核分析(转自某大牛)
- 打钩的动画
- MongoDB shell中执行更新
- 从零开始学习Hadoop--第1章 Hadoop的安装
- unity上面调用andorid相册的插件-android部分
- RelativeLayout
- MongoDB分页、排序与游标
- Java虚拟机详解----常用JVM配置参数
- NYOJ_94 cigarettes 递归VS迭代
- MongoDB索引
- linux数据流重定向
- MongoDB 聚合
- MongoDB 备份