|
@@ -1,202 +0,0 @@
|
|
-package com.gfg.gfglibrary;
|
|
|
|
-
|
|
|
|
-//所需要用到的库
|
|
|
|
-
|
|
|
|
-import android.app.Activity;
|
|
|
|
-import android.content.ContentResolver;
|
|
|
|
-import android.content.ContentValues;
|
|
|
|
-import android.content.Context;
|
|
|
|
-import android.content.Intent;
|
|
|
|
-import android.net.Uri;
|
|
|
|
-import android.os.Environment;
|
|
|
|
-import android.provider.MediaStore;
|
|
|
|
-import android.widget.Toast;
|
|
|
|
-
|
|
|
|
-import androidx.annotation.Nullable;
|
|
|
|
-
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.io.FileInputStream;
|
|
|
|
-import java.io.FileNotFoundException;
|
|
|
|
-import java.io.FileOutputStream;
|
|
|
|
-import java.io.IOException;
|
|
|
|
-import java.io.OutputStream;
|
|
|
|
-import java.lang.reflect.InvocationTargetException;
|
|
|
|
-import java.lang.reflect.Method;
|
|
|
|
-import java.net.FileNameMap;
|
|
|
|
-import java.net.URLConnection;
|
|
|
|
-
|
|
|
|
-public class SaveImage {
|
|
|
|
- public static Context context;
|
|
|
|
- //unity项目启动时的的Activity
|
|
|
|
- private static Activity _unityActivity;
|
|
|
|
- private static String DIRECTORY_WANSHIJING = "Wanshijing";
|
|
|
|
-
|
|
|
|
- //获取unity项目的Activity
|
|
|
|
- private static Activity getActivity() {
|
|
|
|
- if (null == _unityActivity) {
|
|
|
|
- try {
|
|
|
|
- Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
|
|
|
|
- Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);
|
|
|
|
- _unityActivity = activity;
|
|
|
|
- } catch (ClassNotFoundException e) {
|
|
|
|
-
|
|
|
|
- } catch (IllegalAccessException e) {
|
|
|
|
-
|
|
|
|
- } catch (NoSuchFieldException e) {
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return _unityActivity;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 调用Unity的方法
|
|
|
|
- * @param gameObjectName 调用的GameObject的名称
|
|
|
|
- * @param functionName 方法名
|
|
|
|
- * @param args 参数
|
|
|
|
- * @return 调用是否成功
|
|
|
|
- */
|
|
|
|
- boolean callUnity(String gameObjectName, String functionName, String args){
|
|
|
|
- try {
|
|
|
|
- Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
|
|
|
|
- Method method =classtype.getMethod("UnitySendMessage", String.class,String.class,String.class);
|
|
|
|
- method.invoke(classtype,gameObjectName,functionName,args);
|
|
|
|
- return true;
|
|
|
|
- } catch (ClassNotFoundException e) {
|
|
|
|
-
|
|
|
|
- } catch (NoSuchMethodException e) {
|
|
|
|
-
|
|
|
|
- } catch (IllegalAccessException e) {
|
|
|
|
-
|
|
|
|
- } catch (InvocationTargetException e) {
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- @Nullable
|
|
|
|
- File getAppSpecificAlbumStorageDir(Context context, String albumName) {
|
|
|
|
- // Get the pictures directory that's inside the app-specific directory on
|
|
|
|
- // external storage.
|
|
|
|
- File file = new File(context.getExternalFilesDir(
|
|
|
|
- Environment.DIRECTORY_PICTURES), albumName);
|
|
|
|
- if (!file.exists() && !file.mkdirs()) {
|
|
|
|
- Toast.makeText(getActivity(), "Directory not created", Toast.LENGTH_SHORT).show();
|
|
|
|
- }
|
|
|
|
- return file;
|
|
|
|
- }
|
|
|
|
- //更新相册
|
|
|
|
- public static void scanFile(String filePath, String str) {
|
|
|
|
- if(str == null)
|
|
|
|
- str = "已保存至相册";//设置保存成功的提示内容.
|
|
|
|
- context = getActivity().getApplicationContext();
|
|
|
|
- File oldFile = new File(filePath);
|
|
|
|
- String fileName = oldFile.getName();
|
|
|
|
-// File appDir = getAppSpecificAlbumStorageDir(context, "Gfg");
|
|
|
|
-// Toast.makeText(getActivity(), "appDir " + appDir.getPath(), Toast.LENGTH_SHORT).show();
|
|
|
|
-// File file = new File(appDir.getPath(), fileName);
|
|
|
|
- String oldFilePath = oldFile.getPath();
|
|
|
|
-
|
|
|
|
-// Bitmap bitmap = BitmapFactory.decodeFile(filePath);
|
|
|
|
-// FileOutputStream fos = null;
|
|
|
|
-// try {
|
|
|
|
-// fos = new FileOutputStream(file);
|
|
|
|
-// } catch (FileNotFoundException e) {
|
|
|
|
-// // TODO Auto-generated catch block
|
|
|
|
-// Log.w("cat", e.toString());
|
|
|
|
-// }
|
|
|
|
-// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
|
|
|
|
-//
|
|
|
|
-// try {
|
|
|
|
-// fos.flush();
|
|
|
|
-// } catch (IOException e) {
|
|
|
|
-// // TODO Auto-generated catch block
|
|
|
|
-// Log.w("cat", e.toString());
|
|
|
|
-// }
|
|
|
|
-// try {
|
|
|
|
-// fos.close();
|
|
|
|
-// } catch (IOException e) {
|
|
|
|
-// // TODO Auto-generated catch block
|
|
|
|
-// Log.w("cat", e.toString());
|
|
|
|
-// }
|
|
|
|
-// bitmap.recycle();
|
|
|
|
- String mimeType = getMimeType(oldFile);
|
|
|
|
-// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
|
|
|
|
- ContentValues values = new ContentValues();
|
|
|
|
- values.put(MediaStore.MediaColumns.DISPLAY_NAME,fileName);
|
|
|
|
- values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
|
|
|
|
- values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + DIRECTORY_WANSHIJING);
|
|
|
|
- ContentResolver contentResolver = context.getContentResolver();
|
|
|
|
- Uri uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
|
|
|
|
- if(uri == null){
|
|
|
|
- String dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + File.separator + DIRECTORY_WANSHIJING;
|
|
|
|
- File dir = new File(dirPath);
|
|
|
|
- if(!dir.exists())
|
|
|
|
- {
|
|
|
|
- dir.mkdirs();
|
|
|
|
- }
|
|
|
|
- String newFilePath = dirPath + File.separator + fileName;
|
|
|
|
- File newFile = new File(newFilePath);
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- FileOutputStream out = new FileOutputStream(newFile);
|
|
|
|
- FileInputStream fis = new FileInputStream(oldFile);
|
|
|
|
- byte[] data = new byte[1024];
|
|
|
|
- while (fis.read(data) != -1) {
|
|
|
|
- out.write(data);
|
|
|
|
- }
|
|
|
|
- fis.close();
|
|
|
|
- out.close();
|
|
|
|
- context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(newFilePath))));
|
|
|
|
- str += "!";
|
|
|
|
- }
|
|
|
|
- catch (IOException e)
|
|
|
|
- {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- Toast.makeText(getActivity(), "保存失败。", Toast.LENGTH_SHORT).show();
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- try {
|
|
|
|
- OutputStream out = contentResolver.openOutputStream(uri);
|
|
|
|
- FileInputStream fis = new FileInputStream(oldFile);
|
|
|
|
- byte[] data = new byte[1024];
|
|
|
|
- while (fis.read(data) != -1) {
|
|
|
|
- out.write(data);
|
|
|
|
- }
|
|
|
|
- fis.close();
|
|
|
|
- out.close();
|
|
|
|
- str += "!";
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- Toast.makeText(getActivity(), "保存失败!", Toast.LENGTH_SHORT).show();
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-// }
|
|
|
|
-// else
|
|
|
|
-// {
|
|
|
|
-// // 其次把文件插入到系统图库
|
|
|
|
-// try {
|
|
|
|
-// MediaStore.Images.Media.insertImage(context.getContentResolver(),
|
|
|
|
-// newFilePath, fileName, null);
|
|
|
|
-// } catch (FileNotFoundException e) {
|
|
|
|
-// e.printStackTrace();
|
|
|
|
-// Toast.makeText(getActivity(), "insertImage failed", Toast.LENGTH_SHORT).show();
|
|
|
|
-// }
|
|
|
|
-// context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(newFilePath))));
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
- oldFile.delete();
|
|
|
|
- //显示提示
|
|
|
|
- Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public static String getMimeType(File file){
|
|
|
|
- FileNameMap fileNameMap = URLConnection.getFileNameMap();
|
|
|
|
- String type = fileNameMap.getContentTypeFor(file.getName());
|
|
|
|
- return type;
|
|
|
|
- }
|
|
|
|
-}
|
|
|