2010-12-04 95 views
0

我想复制asstets文件夹中的图片并将其粘贴到我的包中,仅供测试。但最终的形象不显示任何东西。当我想用油漆打开图片时,它会说“这不是有效的位图文件”。Android:复制并粘贴图像不起作用!

在我的节目,我开始用这种方法来读取原始图像:

private void copyImage() 
{ 
    AssetManager am = getResources().getAssets();    
    try{ 
     image = BitmapFactory.decodeStream(am.open("tasnim.png")); 
     imWidth = image.getWidth(); 
     imHeight = image.getHeight(); 
    } 
    catch(IOException e){ 
     e.printStackTrace(); 
     System.out.println("Error accoured!"); 
    } 
} 

接下来,我会得到图像的像素或提取像素,并保存像素的整数(rgbstream)的阵列。

private void getPixelsOfImage() 
{ 
    rgbStream = new int[imWidth * imHeight]; 
    image.getPixels(rgbStream, 0, imWidth, 0, 0, imWidth, imHeight); 
} 

最后,我想将它保存在我的包,

private void createPicture() 
{ 
    contextPath = context.getFilesDir().getAbsolutePath(); 
    String path = contextPath + "/" + picName; 

    try{ 
     FileOutputStream fos = new FileOutputStream(path); 
     DataOutputStream dos = new DataOutputStream(fos); 

     for(int i=0; i<rgbStream.length; i++) 
      dos.writeByte(rgbStream[i]); 

     dos.flush(); 
     dos.close(); 
     fos.close(); 
    } 
    catch(IOException e){ 
     System.out.println("IOException : " + e); 
    } 
    System.out.println("Picture Created."); 
} 

代码工作正常,但结果是,没有! :( 当我检查DDMS它创建新的文件和存储所有像素(因为它显示这个文件的大小是13300和我的原始图片的尺寸是100 * 133)。当我点击“从设备拉文件”我可以将它保存在我的桌面上,但是,当我打开它时,没有任何东西。

你觉得呢?我的代码中有没有问题? 谢谢

+0

我几乎肯定,这不是PNG的工作方式。 – Falmarri 2010-12-04 05:39:38

+0

你能解释一下“什么都没有”的意思吗?有文件吗?它有多大?当你看十六进制转储时,有什么内容?你打开它怎么样? – EboMike 2010-12-04 05:40:49

回答

1

我不知道你的意图是什么 - 你想写出一个原始图像文件?

假设你想要写一个JPEG或PNG或什么的,你可以删除你的整个代码,做一些轻松了不少:

Bitmap image = BitmapFactory.decodeStream(am.open("tasnim.png")); 
FileOutputStream fos = new FileOutputStream(path); 
image.compress(Bitmap.CompressFormat.PNG, 100, fos); 

当然了适当的错误检查。