2011-09-05 28 views
0

我從圖庫附加圖像,並顯示其他視圖我附加2次成功完整,但在下一次附加試圖從圖庫附加圖像強制關閉我的應用程序,爲什麼?非常驚訝爲什麼它發生?從圖庫fc附加圖像反覆附加相同的圖像

代碼開放galery

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

選擇路徑代碼是......它顯示給其他活動

 public void onActivityResult(int requestCode, int resultCode, Intent data) { 

      if (requestCode == CAMERA_PIC_REQUEST||requestCode == SELECT_PICTURE) { 
       try{ 
        selectedImageUri = data.getData(); 

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

        //MEDIA GALLERY 
        selectedImagePath = getPath(selectedImageUri); 

        //DEBUG PURPOSE - you can delete this if you want 
        if(selectedImagePath!=null){ 

         Intent i=new Intent(MainMenu.this,Imageprview.class); 
          startActivity(i); 
          System.out.println(selectedImagePath); 
        } 

        else 
         System.out.println("selectedImagePath is null"); 
        if(filemanagerstring!=null) 
         System.out.println(filemanagerstring); 
        else System.out.println("filemanagerstring is null"); 

        //NOW WE HAVE OUR WANTED STRING 
        if(selectedImagePath!=null) 
         System.out.println("selectedImagePath is the right one for you!"); 
        else 
         System.out.println("filemanagerstring is the right one for you!"); 
       }catch (NullPointerException e) { 
        // TODO: handle exception 
       }    } 
     } 

     //UPDATED! 
      public String getPath(Uri uri) { 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     if(cursor!=null) 
     { 
      //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL 
      //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA 
      int column_index = cursor 
      .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } 
     else return null; 
    } 

代碼..

 setContentView(R.layout.imagepreview); 
    ImageView iv=(ImageView)findViewById(R.id.imageView1); 
    bmImg = BitmapFactory.decodeFile(MainMenu.selectedImagePath); 
    iv.setImageBitmap(bmImg); 

它連接兩個或一次,但在接下來的一次試驗之後,它會強行關閉......

在此先感謝

回答

0

我用這個代碼從圖庫中選擇圖像....

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

後,我撥打下面的方法對於獲取圖像數據...

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == 1) { 
      // currImageURI is the global variable I'm using to hold the content:// URI of the image 
      currImageURI = data.getData(); 
      getRealPathFromURI(currImageURI); 
     } 
    } 
    } 


public String getRealPathFromURI(Uri currImageURI) { 
    // can post image 
    String [] proj={MediaStore.Images.Media.DATA}; 
    Cursor cursor = managedQuery(currImageURI, 
      proj, // Which columns to return 
      null,  // WHERE clause; which rows to return (all rows) 
      null,  // WHERE clause selection arguments (none) 
      null); // Order-by clause (ascending by name) 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
     ImageView img_pic = (ImageView)findViewById(R.id.ImageView1); 
     img_pic.setBackgroundDrawable(null); 
     img_pic.setImageURI(currImageURI); 
    return cursor.getString(column_index); 

}