Android:文件写入后无法打开

时间:2014-04-08 08:50:18

标签: android

我试图将图像从资产写入SD卡。它出来但我无法打开它。我写这篇文章有什么错误吗?

这是我的onBackground()代码

    protected Void doInBackground(Void... params) {
    // TODO Auto-generated method stub
    File file = new File(Environment.getExternalStorageDirectory()
                + File.separator + "myDownloadedImage.jpg");
    if (file.exists ()) file.delete ();
    AssetManager asset = context1.getAssets();


    try {
        file.createNewFile();
        Log.v("test", "File Created");
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        InputStream test= asset.open("image.JPG");
        InputStream is = new BufferedInputStream(test, 1024);

        Log.v("test", "InputStream Created");
        byte[] buffer = new byte[1024];
        is.read(buffer);
        OutputStream os = new FileOutputStream(file);
        int read;
        long total = 0;
        Log.v("test", "OutputStream Created");
        while ((read = is.read(buffer)) >= 0) {
            total += read;
            os.write(buffer, 0, read);
            publishProgress((int)((total*100)/2000));
        }

        os.flush();
        os.close();
        is.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

另外,请任何机会告诉我应该在publishProgress()

上放什么 编辑:我检查了输出文件的属性说 类型:文件 路径:/ sdcard / 大小:2.06 MB

0 个答案:

没有答案