2011-01-25 67 views
0

我通過意圖開始畫廊,否則它給網格視圖中顯示畫廊的問題。 但我想從圖庫中選擇圖像的實際SD卡路徑,意圖打開。意圖畫廊的圖像SD卡路徑

這裏是代碼..

Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT); 
    photoPickerIntent.setType("image/jpg"); 
    photoPickerIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Pictures/image.jpg")); 
    startActivityForResult(photoPickerIntent, 1); 



protected void onActivityResult(int requestCode, int resultCode, Intent intent)  { 
    super.onActivityResult(requestCode, resultCode, intent); 

      if (resultCode == RESULT_CANCELED) { 
      showToast(this,"Activity cancelled"); 
      return; 
      } 
      else if(resultCode == RESULT_OK) { 


      System.out.println("requestCode--"+requestCode); 
      System.out.println("resultCode--"+resultCode); 
      System.out.println("data--"+intent.getData()); 
      Uri uri = intent.getData(); 
      String data = uri.getPath(); 
      System.out.println("uri.getPath()--"+uri.getPath()); 
      System.out.println("type--"+intent.getType()); 
      System.out.println("path--"+Environment.getExternalStorageState()); 


       return; 

       } 

      switch (requestCode) { 
       case CAMERA_ACTIVITY: 
        Bundle b = intent.getExtras(); 
        Bitmap bm = (Bitmap) b.get("data"); 
       //  mImageView.setImageBitmap(bm); // Display image in the View 

        // large image? 
         if (b.containsKey(MediaStore.EXTRA_OUTPUT)) { // large image? 
         Log.i(TAG, "This is a large image"); 
          showToast(this,"Large image"); 
         // Should have to do nothing for big images -- should already saved in MediaStore ... but 
          MediaStore.Images.Media.insertImage(getContentResolver(), bm, null, null); 
          } 
          else { 
           Log.i(TAG, "This is a small image"); 
           showToast(this,"Small image"); 
           MediaStore.Images.Media.insertImage(getContentResolver(), bm, null, null); 
          } 
          break; 
         } 
        } 
       } 

我從意向對象獲取的所有數據。但我想圖像的SD卡路徑,我選擇上傳圖像在服務器..

我怎麼得到這個?

回答

1

你可能想是這樣的:

Uri uri = (Uri) intent.getExtras().get("android.intent.extra.STREAM"); 

if (intent.getAction().equals(Intent.ACTION_SEND)) 
{ 
    if (uri.getScheme().equals("content")) 
    { 
     Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
     cursor.moveToFirst(); 
     filePath = cursor.getString(cursor.getColumnIndexOrThrow(Images.Media.DATA)); 
    } 
} 
0

本準則可以幫助你

該代碼會彈出一個對話框用相機和畫廊和設備的SD卡來獲取圖像的路徑。

import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Random; 

import android.app.Activity; 
import android.app.Dialog; 
import android.content.Intent; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.Button; 

import com.fanosedu.R; 


public class FileAttachPopUpUtils extends Activity { 

    private View rootView; 
    private Dialog popup; 
    private Button Assig_PopUp_DeviceBtn; 
    private Button Assig_PopUp_GalaryBtn; 
    private Button Assi_PopUp_CameraBtn; 
    private Button Assig_PopUp_CancelPopupBtn; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // setContentView(R.layout.assignment_popupfile); 

     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

     /* 
     * getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     * WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     */ 
     popup = new Dialog(this); 
     // popup.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     // popup.getWindow().setBackgroundDrawable(new 
     // ColorDrawable(android.graphics.Color.TRANSPARENT)); 
     popup.setContentView(R.layout.assignment_popupfile); 

     popup.setCanceledOnTouchOutside(false); 

     popup.show(); 

     Assig_PopUp_DeviceBtn = (Button) popup 
       .findViewById(R.id.Assignment_PopUp_DeviceBtn); 

     Assig_PopUp_GalaryBtn = (Button) popup 
       .findViewById(R.id.Assignment_PopUp_GalaryBtn); 
     Assi_PopUp_CameraBtn = (Button) popup 
       .findViewById(R.id.Assignment_PopUp_CameraBtn); 

     Assig_PopUp_CancelPopupBtn = (Button) popup 
       .findViewById(R.id.Assignment_PopUp_CancelPopupBtn); 

     Assig_PopUp_DeviceBtn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       /* 
       * Toast.makeText(FileAttachPopUpUtils.this, 
       * "Device File In-Progress", Toast.LENGTH_SHORT) .show(); 
       */ 
       Intent intent = new Intent(FileAttachPopUpUtils.this, 
         FileExplore.class); 
       startActivity(intent); 
       popup.dismiss(); 
       finish(); 

      } 
     }); 

     Assig_PopUp_GalaryBtn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Intent intent = new Intent(
         Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(intent, 2); 

      } 
     }); 
     Assi_PopUp_CameraBtn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       // /////////////////////////////////////////////////////////////// 

       Intent intent = new Intent(
         android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(intent, 1); 
      } 
     }); 

     Assig_PopUp_CancelPopupBtn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       popup.dismiss(); 
       FileAttachPopUpUtils.this.finish(); 
      } 
     }); 

    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == this.RESULT_OK) { 
      if (requestCode == 1) { 

       /* 
       * Intent intent = new Intent(Intent.ACTION_PICK, 
       * android.provider 
       * .MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       * startActivityForResult(intent, 2); 
       */ 

       Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
       // mImage.setImageBitmap(thumbnail); 
       // 3 
       ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
       thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
       // 4 
       String name; 
       int n = 100000; 
       int rand; 
       rand = new Random().nextInt(n); 
       name = "Image-" + rand + ".jpg"; 
       // File fileimage = new File(path, name); 
       File file = new File(Environment.getExternalStorageDirectory() 
         + File.separator + name); 

       System.out.println("FILE PATH=======>>>>>>>>>>" 
         + file.toString()); 
       try { 
        file.createNewFile(); 
        FileOutputStream fo = new FileOutputStream(file); 
        // 5 
        fo.write(bytes.toByteArray()); 
        fo.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       Assignments.mulPathArry.add(file.toString()); 
       // Here You will get the full path 

       FileAttachPopUpUtils.this.finish(); 

      } else if (requestCode == 2) { 

       Uri selectedImage = data.getData(); 
       String[] filePath = { MediaStore.Images.Media.DATA }; 
       Cursor c = this.getContentResolver().query(selectedImage, 
         filePath, null, null, null); 
       c.moveToFirst(); 
       int columnIndex = c.getColumnIndex(filePath[0]); 
       String picturePath = c.getString(columnIndex); 
       c.close(); 

       Assignments.mulPathArry.add(picturePath); 
       // Here you will get Path 

       popup.dismiss(); 
       FileAttachPopUpUtils.this.finish(); 
      } 
     } 

     // /////////////////////////////////////////////////////////////////////// 

    } 

    @Override 
    public void onBackPressed() { 
     return; 
    } 

}