【问题标题】:Moving asset to /data/data/APP_NAME/files将资产移动到 /data/data/APP_NAME/files
【发布时间】:2011-05-02 15:13:34
【问题描述】:

我所读内容的快速参考 http://thedevelopersinfo.com/2009/11/17/using-assets-in-android/

http://www.wiseandroid.com/post/2010/06/14/Android-Beginners-Intro-to-Resources-and-Assets.aspx

还有更多,但作为新手只能发2个。

在我的应用程序中有一个按钮,如果用户决定在有根设备上这样做,它将重新启动到引导加载程序。我有一个名为“rebo​​ot”的重启二进制文件,它将允许命令运行,它位于 /assets/ 中。使用上述方法,我似乎无法“重新启动”来移动甚至在我的 apk 的 /data/data/ 中创建目录“文件”。我的问题是,是否有更好的指导来指导我学习这门学科,或者这些是最好的,我只是头脑清醒地理解它。或者,如果您有其他一些示例代码,我可以通读并尝试理解将是完美的。谢谢。

添加了我在做什么的示例

$ public static String moveReboot = "/data/data/com.DPE.MuchSuck/Files";
public static String reboot = "file:///android_asset/reboot";
public static Context myContext;

@Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
        moveFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
$ public void moveFile() throws IOException {
  AssetManager assetManager = getAssets();
  InputStream myInput = assetManager.open(reboot);
  String outFileName = moveReboot + reboot;
  OutputStream myOutput = new FileOutputStream(outFileName);
  byte[] buffer = new byte [1024];
  int length;
  while ((length = myInput.read(buffer))>0){
   myOutput.write(buffer, 0, length);

  }
  myOutput.flush();
  myOutput.close();
  myInput.close();

在运行 apk 时,“文件”中没有任何显示,“文件甚至没有显示。

【问题讨论】:

    标签: android import device assets


    【解决方案1】:

    道格,

    看起来您的“outFileName”不包含一个好的文件名。 尝试使用 getExternalFilesDir() 获取 SDRAM 的路径。

    见:http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir%28java.lang.String%29

    编辑...

    道格,这是我的实现

    private void copyAssetToSDRAM(String strFilename)
    {
        try
        {
            //complete path to target file
            File fileTarget = new File(Environment.getExternalStorageDirectory(), strFilename);
    
            //data source stream
            AssetManager assetManager = ApplicationContext.getContext().getAssets();
            InputStream istr = assetManager.open(strFilename);
    
            //data destination stream
            //NOTE: at this point you'll get an exception if you don't have permission to access SDRAM ! (see manifest)
            OutputStream ostr = new FileOutputStream(fileTarget);
    
            byte[] buffer = new byte[1024];
            int length;
            while ((length = istr.read(buffer))>0)
            {
                ostr.write(buffer, 0, length);
            }
            ostr.flush();
            ostr.close();
            istr.close();
    
        }
        catch(Exception e)
        {
            Toast.makeText(ApplicationContext.getContext(), "File-Copy Error: "+strFilename, Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
    

    要使用它,请执行以下操作:

            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            {
                copyAssetToSDRAM("myfile.png");
            }
            else
            {
                Toast.makeText(ApplicationContext.getContext(), "Unable to copy images. NO SDRAM", Toast.LENGTH_LONG).show();
            }
    

    【讨论】:

    • 感谢您的意见,我会调查的。
    • 嗨,道格,我已经修改了上面的答案。该代码应该很容易插入到您的代码中。如果能解决您的问题,请采纳我的回答,谢谢!
    • 虽然答案提供了一个可行的解决方案,但代码并不是 100% 安全“插入”的。处理外部资源 (IO) 时的第一条规则,在 finally 块中关闭流,不要在“try”内。
    猜你喜欢
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 2017-10-03
    • 2014-04-14
    • 2012-03-31
    相关资源
    最近更新 更多