2011-06-06 44 views
1

我使用gridview示例与图像适配器来呈现图像,区别在于这些图像是从sdcard中的特定文件夹中检索的,例如,/SD卡/图像。 我正在emulator上测试这个应用程序。为此,我首先在模拟器上配置了sdcard,然后通过eclipse下的DDMS在这个特定的文件夹上推送图像。如何在设备上安装应用程序时在包含图像的SD卡中创建文件夹?

我想知道的是,当用户在真实设备上安装应用程序时,有可能在SD卡中创建包含图像的图像文件夹,并且如果可能的话,该怎么做?

回答

0

我不知道有办法做到这一点(这并不是说它不能完成)。 您绝对可以做的一件事就是在用户第一次运行应用程序时创建文件夹,并将其填入图像中。

0

您可以从资产图像​​复制到SD卡

这种方法从资产文件夹复制到你的SD卡拷贝图像。这里Jaydeep的文件夹是可以在那个地方使用您的文件夹名称,我对sdcard.You文件夹的名称。

public void copyImagesInSdcard() 
{ 
assetManager = mycontext.getAssets(); 
assetManager1 = mycontext.getAssets(); 
System.out.println("In copyImagesInSdcard"); 
try 
{ 
    str1=assetManager.list(""); 
    ss=assetManager1.list(str1[1]); 
    InputStream is; 
    //System.out.println(ss[0]); 
    File file=new File(Environment.getExternalStorageDirectory().getPath() + "/Jaydeep's Folder"); 
    if(file.exists()!=true) 
    { 
    file.mkdir(); 
    } 
    else 
    { 
     if(file.length()==0) 
     { 
      file.mkdir(); 
     } 
     System.out.println("Length:"+file.length()); 
    } 

     for(int i=0;i<ss.length;i++) 
     { 
     is=assetManager1.open(str1[1] + "/" + ss[i]); 


     file=new File(Environment.getExternalStorageDirectory().getPath() + "/Jaydeep's Folder/" + ss[i]); 

     FileOutputStream out=new FileOutputStream(file); 
//Bitmap bi = BitmapDrawable.createFromStream(is,ss[0]); 


     byte b[] =new byte[4096]; 
     while (is.read(b) != -1) { 

      out.write(b); 
      } 
      out.close();   
     } 

} 
catch (IOException e) 
{ 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
} 
+0

感谢jaydeep的答复。我有一个问题。如果子文件夹内有资源,如myapp/folder1/folder2/image /。图像是在图像文件夹内我将不得不添加像这样的getAssets()中的图像的路径assetManager1 = mycontext.getAssets()。“/ myapp/folder1/folder2/image /”; – 2011-06-06 13:32:09

+0

我没有尝试嵌套的文件夹,但我认为它应该为那工作。如果路径设置为那么就不会发生错误 – 2011-06-07 04:37:31

+0

您好jaydeep我已经在我的应用程序上面的代码尝试过,但没有发生。在assets文件夹中添加一个png文件,并使用上面的代码来检查这个文件是否被转移到/ sdcard/demo但是这个文件没有被添加 – 2011-06-07 06:21:39

相关问题