2012-03-17 174 views
1

我在我的Android應用程序中使用了andengine,並且想要顯示設備圖庫中的圖像。我如何獲得圖像路徑? 請注意,我不希望使用這樣的事:Android中的圖像路徑

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

我需要什麼,是圖像的人的路。你能幫我嗎?

回答

2

試試這個代碼,而無需使用意圖,

private void FillPhotoList() 
{ 
    // initialize the list! 
    ArrayList<String> GalleryList = new ArrayList<String>(); 
    String[] projection = {MediaStore.Images.ImageColumns.DATA}; 
    // intialize the Uri and the Cursor, and the current expected size. 
    Cursor c = null; 
    Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
    // 
    // Query the Uri to get the data path. Only if the Uri is valid. 
    if (u != null) 
    { 
     c = managedQuery(u, projection, null, null, null); 
    } 
    // If we found the cursor and found a record in it (we also have the id). 
    if ((c != null) && (c.moveToFirst())) 
    { 
     do 
     { 
      // Loop each and add to the list. 
      GalleryList.add(c.getString(0)); 
     } 
     while (c.moveToNext()); 
    } 
} 
+0

非常感謝 – 2012-03-17 11:48:56

7

請看下面的代碼,

public class select_Gallery_image extends Activity{ 

    private static final int SELECT_PICTURE = 1; 
    protected String _path; 
    protected boolean _taken; 
    protected static final String PHOTO_TAKEN = "photo_taken"; 
    ProgressDialog PD1; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     Intent intent = new Intent(Intent.ACTION_PICK); 
     intent.setType("image/*"); 
     //intent.setAction(Intent.ACTION_GET_CONTENT); 
     startActivityForResult(intent, SELECT_PICTURE); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     if (resultCode == RESULT_OK) 
     { 
      if (requestCode == SELECT_PICTURE) 
      { 
       settings.bitmap=null; 
       Uri selectedImageUri = data.getData(); 
       settings.selectedImagePath = getPath(selectedImageUri); 
       File filenew = new File(settings.selectedImagePath); 
       int file_size = Integer.parseInt(String.valueOf(filenew.length()/1024)); 
       if(file_size<= 10000){ 
       PD1 = ProgressDialog.show(select_Gallery_image.this,"","Loading"); 
       new Thread(new Runnable() { 

        public void run() { 
         Looper.prepare(); 
         settings.bitmap = decodeFile(settings.selectedImagePath); 
         imagehandler.sendEmptyMessage(0); 

        } 
       }).start(); 
       } 
       else{ 
        AlertDialog.Builder alertbox = new AlertDialog.Builder(select_Gallery_image.this); 
        alertbox.setMessage("Take Image Size Less than 10 MB"); 
        alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface arg0, int arg1) { 
         finish(); 
         } 
        }); 
        alertbox.show(); 
       } 
      } 
     } 
    } 
    private Handler imagehandler = new Handler(){ 
      public void handleMessage(Message msg){ 
      PD1.dismiss(); 
      settings.img_logo.setImageBitmap(settings.bitmap); 
      settings.img_logo.setBackgroundColor(Color.DKGRAY); 
       finish(); 
      } 
    }; 


    public String getPath(Uri uri) 
    { 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 

    } 

    static Bitmap decodeFile(String str){ 
      try { 
       //decode image size 
       BitmapFactory.Options o = new BitmapFactory.Options(); 
       o.inJustDecodeBounds = true; 
       BitmapFactory.decodeStream(new FileInputStream(str),null,o); 

       //Find the correct scale value. It should be the power of 2. 
       final int REQUIRED_SIZE=70; 
       int width_tmp=o.outWidth, height_tmp=o.outHeight; 
       int scale=1; 
       while(true){ 
        if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
         break; 
        width_tmp/=2; 
        height_tmp/=2; 
        scale++; 
       } 

       //decode with inSampleSize 
       BitmapFactory.Options o2 = new BitmapFactory.Options(); 
       o2.inSampleSize=scale; 
       return BitmapFactory.decodeStream(new FileInputStream(str), null, o2); 
      } catch (FileNotFoundException e) {} 
      return null; 
     } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if(keyCode == KeyEvent.KEYCODE_BACK){ 
      finish(); 
     } 
     return super.onKeyDown(keyCode, event); 
    } 
} 
1

也許我誤解了這個問題在這裏,但像你想它的聲音,我知道如何在默認情況下查找照片的存儲位置。

Dev Guide

在API級別8或更高,使用getExternalStoragePublicDirectory(),通過它你要公開目錄,如DIRECTORY_MUSIC,DIRECTORY_PICTURES,DIRECTORY_RINGTONES,或其他的類型。如果需要,此方法將創建適當的目錄。

如果您使用API​​ 7級或更低,使用getExternalStorageDirectory()打開表示外部存儲的根文件,然後保存在以下目錄中的一個共享文件:

Music/ - Media scanner classifies all media found here as user music. 
Podcasts/ - Media scanner classifies all media found here as a podcast. 
Ringtones/ - Media scanner classifies all media found here as a ringtone. 
Alarms/ - Media scanner classifies all media found here as an alarm sound. 
Notifications/ - Media scanner classifies all media found here as a notification sound. 
Pictures/ - All photos (excluding those taken with the camera). 
Movies/ - All movies (excluding those taken with the camcorder). 
Download/ - Miscellaneous downloads.