现步骤:
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=""; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.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.书写打开相册选择图片之后的回调方法
@Override protected 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包,并且导出相关的资源文件