unity上面呼叫andorid相簿的外掛-android部分
阿新 • • 發佈:2019-01-04
由於最近專案需要做,一個簡單的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的類
3.主要的類中書寫供unity腳本回調的方法(Main()是實現開啟相簿的方法,RefreshPic是實現圖片截圖之後儲存到照相機中的相簿方法)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); } }
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.書寫開啟相簿選擇圖片之後的回撥方法
5.書寫回調圖片方法之後的相關工具方法(檔案複製,壓縮,儲存圖片等)@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); }
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包,並且匯出相關的資原始檔