2013-11-28 154 views
1

am使用活動作爲對話框。在那裏我有按鈕來瀏覽並選擇圖像以在圖像視圖中顯示它。從SD卡中瀏覽圖像以在android的圖像視圖中顯示它?

我的對話活動代碼和瀏覽圖像

public class Update_profile extends Activity{ 

private Uri mImageCaptureUri; 

private static final int PICK_FROM_FILE = 1; 

private View rootView; 
Button save,browse_image; 
ImageView profile_pic; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setBackgroundDrawable(new ColorDrawable(0)); 
    setContentView(R.layout.update_profile); 

    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { 
     @Override 
     public void uncaughtException(Thread t, Throwable e) { 
      android.os.Process.killProcess(android.os.Process.myPid()); 
      System.exit(0); 
     } 
    }); 

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 

    this.rootView=findViewById(R.id.update_profile_details); 
    profile_pic = (ImageView)findViewById(R.id.update_profile_picture); 

    save = (Button)findViewById(R.id.save); 
    browse_image = (Button)findViewById(R.id.browse_image); 
    browse_image.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Intent intent = new Intent(); 

      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 

      startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE); 

     } 
    }); 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode != RESULT_OK) return; 

    Bitmap bitmap = null; 
    String path  = ""; 

    if (requestCode == PICK_FROM_FILE) { 
     mImageCaptureUri = data.getData(); 
     path = getRealPathFromURI(mImageCaptureUri); //from Gallery 
     Log.v("Path", ""+path); 

     if (path == null) 
      path = mImageCaptureUri.getPath(); //from File Manager 
     Log.v("Path", ""+path); 

     if (path != null) 
      bitmap = BitmapFactory.decodeFile(path); 
    } else { 
     path = mImageCaptureUri.getPath(); 
     Log.v("Path", ""+path); 
     bitmap = BitmapFactory.decodeFile(path); 
    } 

    profile_pic.setImageBitmap(bitmap);  
} 

public String getRealPathFromURI(Uri contentUri) { 
    String [] proj  = {MediaStore.Images.Media.DATA}; 
    Cursor cursor  = managedQuery(contentUri, proj, null, null,null); 

    if (cursor == null) return null; 

    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 

    cursor.moveToFirst(); 

    return cursor.getString(column_index); 
} 

@SuppressLint("NewApi") 
@Override 
public boolean onTouchEvent(MotionEvent event) { 
    Rect rect = new Rect(); 
    rootView.getHitRect(rect); 
    if (!rect.contains((int)event.getX(), (int)event.getY())){ 
     setFinishOnTouchOutside(false); 
      return true; 
    } 
     return false;  
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK)) { 

     finish(); 
    } 
    return true; 
} 
} 

我的清單代碼。當我使用上述普通的活動方式瀏覽圖片的代碼更改活動作爲一個對話框

<activity 
     android:name="test.Update_profile" 
     android:excludeFromRecents="true" 
     android:launchMode="singleInstance" 
     android:screenOrientation="portrait" 
     android:theme="@android:style/Theme.Dialog" 
     android:windowSoftInputMode="stateHidden" > 
</activity> 

它獲取的圖像,並在圖像視圖中顯示,但在這個對話框activity.its現在能夠選擇圖像。我想拍照從對話activity.am圖像不能夠解決這個問題。任何人都可以幫助我解決這個問題。

+0

如果我給UA然後你可以在網格視圖中設置所有圖像,然後你也可以在點擊網格視圖元素時顯示你的圖像 –

+0

@BhanuSharma我想要圖像的路徑。 – Yugesh

+0

ohk看到我的答案我給你所有的細節文件路徑也哥們:) –

回答

1

如果u想以檢索從GALLARY直接再使用此代碼 http://viralpatel.net/blogs/pick-image-from-galary-android-app/

這是我從SD卡獲取所有的文件和你剛剛把它保存在一個DataController類類樂施會的花花公子方法

private void getallimages() 
      { 
       String[] STAR = { "*" }; 


       final String[] columns = { MediaStore.Images.Thumbnails._ID , MediaStore.Images.Media.DATA}; 
       final String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER; 
       Cursor imagecursor = ((Activity) cntx).managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STAR, null, null, orderBy); 
       int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID); 
       int imgNameIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME); 
       int count = imagecursor.getCount(); 
       for (int i = 0; i < count; i++) { 
        imagecursor.moveToPosition(i); 
        int id = imagecursor.getInt(image_column_index); 
        ImageItem imageItem = new ImageItem(); 
        imageItem.id = id; 
        imageItem.filePath = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA)); 
        lastId = id; 
        imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(cntx.getContentResolver(), id,MediaStore.Images.Thumbnails.MICRO_KIND, null); 
        imageItem.selection = false; //newly added item will be selected by default 
        controller.images.add(imageItem); 
       } 

      } 

這是的ImageItem包裝類

public class ImageItem { 
      public boolean selection=true; 
      public int id; 
      public Bitmap img; 
      public String filePath; 
      public String fileType; 
    } 
+1

這是什麼** lastId(它的上一圖像路徑)**。 – Yugesh

+0

只是刪除這行忘了這個因爲這是我用來維護列表視圖中的位置,所以你只是避免這 –

+0

thanks.it檢索所有圖像path.but我需要選擇性的圖像路徑(在我的問題中說什麼)。 – Yugesh

0
You can try this : 

    public class EMView extends Activity { 
    ImageView img,img1; 
    int column_index; 
    Intent intent=null; 
    // Declare our Views, so we can access them later 
    String logo,imagePath,Logo; 
    Cursor cursor; 
    //YOU CAN EDIT THIS TO WHATEVER YOU WANT 
    private static final int SELECT_PICTURE = 1; 

    String selectedImagePath; 
    //ADDED 
    String filemanagerstring; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     img= (ImageView)findViewById(R.id.gimg1); 



    ((Button) findViewById(R.id.Button01)) 
    .setOnClickListener(new OnClickListener() { 

     public void onClick(View arg0) { 

      // in onCreate or any event where your want the user to 
      // select a file 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(Intent.createChooser(intent, 
        "Select Picture"), SELECT_PICTURE); 


     } 
    }); 
} 

//UPDATED 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == SELECT_PICTURE) { 
      Uri selectedImageUri = data.getData(); 

      //OI FILE Manager 
      filemanagerstring = selectedImageUri.getPath(); 

      //MEDIA GALLERY 
      selectedImagePath = getPath(selectedImageUri); 


      img.setImageURI(selectedImageUri); 

      imagePath.getBytes(); 
      TextView txt = (TextView)findViewById(R.id.title); 
      txt.setText(imagePath.toString()); 


      Bitmap bm = BitmapFactory.decodeFile(imagePath); 

      img1.setImageBitmap(bm); 



     } 

    } 

} 

//UPDATED! 
public String getPath(Uri uri) { 
String[] projection = { MediaColumns.DATA }; 
Cursor cursor = managedQuery(uri, projection, null, null, null); 
column_index = cursor 
     .getColumnIndexOrThrow(MediaColumns.DATA); 
cursor.moveToFirst(); 
imagePath = cursor.getString(column_index); 

return cursor.getString(column_index); 
} 

} 
+0

對於簡單的活動,你的答案工作正常,但不是對話(使用活動作爲對話)。 – Yugesh

相關問題