我使用JNI开发我的应用程序,并且在C ++层中有两个.dat文件用作输入文件。目前,我在打开相关应用之前通过adb将这两个文件推送到移动设备。我认为有一个更好的解决方案可以防止将两个文件推入移动设备。
答案 0 :(得分:0)
public class CameraPreviewActivity extends AppCompatActivity
implements CameraPermissionHelper.CameraPermissionCallback {
SharedPreferences prefs = null;
public static String TAG = "CameraPreviewActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = getSharedPreferences("com.yourcompany.yourapp", MODE_PRIVATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
if(prefs.getBoolean("firstrun", true)){
prefs.edit().putBoolean("firstrun", false).commit();
try {
final InputStream input = getResources().getAssets().open("face_model.dat");
try {
File UPLOAD_DIR = new File("/sdcard");
File file = new File(UPLOAD_DIR, "face_model.dat");
OutputStream output = new FileOutputStream(file);
try {
try {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
} finally {
output.close();
}
} catch (Exception e) {
e.printStackTrace(); // handle exception, define IOException and others
}
} finally {
input.close();
}
}catch (IOException e){
e.printStackTrace();
}
try {
final InputStream input = getResources().getAssets().open("shape_pred.dat");
try {
File UPLOAD_DIR = new File("/sdcard");
File file = new File(UPLOAD_DIR, "shape_pred.dat");
OutputStream output = new FileOutputStream(file);
try {
try {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
} finally {
output.close();
}
} catch (Exception e) {
e.printStackTrace(); // handle exception, define IOException and others
}
} finally {
input.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
}