2014-03-04 22 views
0

我有以下用於瀏覽按鈕點擊上的圖庫的代碼。在android按鈕上瀏覽SD卡點擊

loadFile.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 

     Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(i,RESULT_LOAD_IMAGE); 
    } 
}); 

取而代之,我需要通過點擊按鈕訪問SD卡。 我編輯了代碼:

Intent i = new Intent(Environment.getExternalStorageDirectory().getAbsolutePath()); 

我得到了錯誤。我是android新手。我該怎麼做?請幫幫我。

+0

那麼是什麼問題?你的代碼顯示你已經實現了按鈕點擊。 – Piyush

+0

其展示畫廊。相反,我需要存儲(SD卡)。 – androidGenX

+0

你想打開一個特定的位置,或者只是從父目錄中瀏覽 @PiyushGupta認爲他需要打開一個位置或瀏覽sdcard而不是圖庫。 – user2450263

回答

0

你不能用這個。

Intent i = new Intent(Environment.getExternalStorageDirectory().getAbsolutePath()); 

你應該改變這種

String myFilepath = Environment.getExternalStorageDirectory().getAbsolutePath(); 

File f = new File(myFilepath); 

添加權限您的清單文件。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
+0

點擊我應該能夠瀏覽SD卡的位置 – androidGenX

0
// try this way 
final private int PICK_IMAGE = 1; 
private String selectedImage; 

Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE); 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == Activity.RESULT_OK) { 
      if (requestCode == PICK_IMAGE) { 
       selectedImage = getAbsolutePath(data.getData()); 
       yourImageView.setImageBitmap(decodeFile(selectedImage)); 
      } else { 
       super.onActivityResult(requestCode, resultCode, data); 
      } 
     } 

} 

public String getAbsolutePath(Uri uri) { 
     String[] projection = { MediaColumns.DATA }; 
     @SuppressWarnings("deprecation") 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     if (cursor != null) { 
      int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } else 
      return null; 
} 

public Bitmap decodeFile(String path) { 
     try { 
      // Decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(path, o); 
      // The new size we want to scale to 
      final int REQUIRED_SIZE = 70; 

      // Find the correct scale value. It should be the power of 2. 
      int scale = 1; 
      while (o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE) 
       scale *= 2; 

      // Decode with inSampleSize 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = scale; 
      return BitmapFactory.decodeFile(path, o2); 
     } catch (Throwable e) { 
      e.printStackTrace(); 
     } 
     return null; 

} 
+0

我需要從sd卡讀取文本文件而不是圖像文件。 – androidGenX