2016-08-01 75 views
0

我正試圖從指定路徑中的目錄加載所有圖像。使用if語句檢查被調用的路徑不會發出任何錯誤,也不會顯示任何結果。不介意開始知道我在哪裏得到我的錯誤代碼。從目錄中讀取圖像

public class Edit extends Activity { 

    // File representing the folder that you select using a FileChooser 
    static final File dir = new File("/data/data/faceemoji.alexcz.yourfaceemoji/app_imageDir/"); 

    // array of supported extensions (use a List if you prefer) 
    static final String[] EXTENSIONS = new String[]{ 
      "png" // and other formats you need 
    }; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_edit); 

     LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearlayout); 

     Log.d("loadfilepath", dir.getAbsolutePath()); 
     if (dir.isDirectory()) { // make sure it's a directory 
      System.out.println("yes"); 
      for (File f : dir.listFiles(IMAGE_FILTER)) { 

       Log.d("fak", "file found"); 
       Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath()); 
       ImageView myImage = new ImageView(this); 
       myImage.setImageBitmap(myBitmap); 
       linearLayout.addView(myImage); 
      } 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_edit, menu); 
     return true; 
    } 

    static final FilenameFilter IMAGE_FILTER = new FilenameFilter() { 

     @Override 
     public boolean accept(final File dir, final String name) { 
      for (String ext : EXTENSIONS) { 
       if (name.endsWith("." + ext)) { 
        return (true); 
       } 
      } 
      return (false); 
     } 
    }; 
} 
+0

檢查此[鏈接](HTTP:// WWW。 androhub.com/select-and-share-multiple-images/)。 –

回答

1

您應該使用Context.getFilesDir()內部存儲和Environment.getExternalStorageDirectory()外部存儲,使用必須添加WRITE_EXTERNAL_STORAGE,如果你也想將文件寫入這最後一個。

在你的情況我想你應該做這樣的:

static final File dir = new File(Context.getFilesDir()+"/app_imageDir/"); 

即使thoug把日誌檢查該目錄是正確