2012-01-31 33 views
0

我有大約300-400張圖片,尺寸爲320x480像素jpg格式。我必須在SD卡上添加這些圖像。目前我將這些圖像放在我的項目的Drawable文件夾中。現在我想在SD卡上移動這些圖像,並且我希望將這些圖像用於動畫目的。我還想在我的應用程序首次啓動時檢查是否安裝了SD卡。我沒有任何想法在Android的SD卡編程,所以請任何人有任何想法或初學者的鏈接請讓我知道。如何在Android的SD CARD上載/添加圖像?

CODE

ImageView imgAssets; 
String path=""; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    path = Environment.getExternalStorageDirectory().toString(); 

    try { 
     copyFromAsstesToSDCard(); 
     readFromSDCard(); 
    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 
} 

public void readFromSDCard() { 
    // TODO Auto-generated method stub 
    File dir = Environment.getExternalStorageDirectory(); 
    //File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext"); 

    //Get the text file 
    File file = new File(dir.getAbsolutePath()+ "/cat_angry10.jpg"); 
    // i have kept text.txt in the sd-card 
    //System.out.println(file.toString()); 

    if(file.exists()) // check if file exist 
    { 
     try { 
      Bitmap bitMap = BitmapFactory.decodeFile(Environment.getExternalStorageState() + "/cat_angry10.jpg"); 
      //imgAssets.setImageBitmap(bitMap); 
       imgAssets.setBackgroundDrawable(new BitmapDrawable(bitMap)); 
     } 
     catch (Exception e) { 
      //You'll need to add proper error handling here 
      e.printStackTrace(); 
     } 
    } 
    else 
    { 
     System.out.println("Sorry file doesn't exist!!"); 
    } 

} 

public void copyFromAsstesToSDCard() { 
    // TODO Auto-generated method stub 
    AssetManager assetManager = getAssets(); 
    String[] files = null; 
    try { 
     files = assetManager.list("Images"); 
    } catch (IOException e) { 
     Log.e("tag", e.getMessage()); 
    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 

    for(String filename : files) { 
     System.out.println("File name => "+filename); 
     InputStream in = null; 
     OutputStream out = null; 
     try { 
      in = assetManager.open("Images/"+filename); // if files resides inside the "Files" directory itself 
      out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + "/" + filename); 
      System.out.println(out.toString()); 
      copyFile(in, out); 
      in.close(); 
      in = null; 
      out.flush(); 
      out.close(); 
      out = null; 
     } catch(Exception e) { 
      Log.e("tag", e.getMessage()); 
     } 
    } 
} 

private void copyFile(InputStream in, OutputStream out) { 
    // TODO Auto-generated method stub 
    byte[] buffer = new byte[1024]; 
    int read; 
    try { 
     while((read = in.read(buffer)) != -1){ 
      out.write(buffer, 0, read); 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

從SD卡讀取圖像時,我得到的NullPointerException。

感謝 Keyur

回答

3
+0

感謝您的快速反應.... – Scorpion 2012-01-31 06:37:09

+0

我已經在我的問題添加的代碼片段。我得到NullPointerException在我設置位圖到ImageView的行。 – Scorpion 2012-02-02 16:25:08