2014-03-04 3 views
0

버튼 클릭시 갤러리를 탐색 할 수있는 다음 코드가 있습니다.안드로이드 버튼을 클릭하여 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()); 

오류가 발생했습니다. 나는 안드로이드를 처음 접한다. 내가 어떻게 해? 도와주세요. 당신이 사용할 수 없습니다

+0

그래서 문제는 무엇인가? 버튼 클릭시 구현 한 코드가 표시됩니다. – Piyush

+0

그것의 보여주는 갤러리. 대신 스토리지 (SD 카드)가 필요합니다. – androidGenX

+0

특정 위치를 열거 나 부모 디렉토리에서 탐색 하시겠습니까? @PiyushGupta는 갤러리 대신 위치를 열거 나 sdcard를 찾아야한다고 생각합니다. – user2450263

답변

0

에 ACTION_MEDIA_MOUNTED

청약을보기 사용하려고합니다.

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

관련 문제