unity上面调用andorid相册的插件-android部分

该博客介绍了如何在Unity3D项目中与Android原生代码集成,实现从相册选择图片并进行压缩保存。具体步骤包括从Unity导出jar包,创建继承自UnityPlayerActivity的自定义Activity,实现回调方法处理图片选取,以及相关文件操作如复制和删除。
摘要由CSDN通过智能技术生成

现步骤:

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包,并且导出相关的资源文件


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不吃鸳鸯锅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值