2011-02-01 168 views
1

我能够打开画廊并获得画廊的路径= 内容://媒体/外部/图像/媒体/ 2 但不能在imageview的 解码,这是我的代码无法从图库中选取照片?

公共无效onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main);

  b=(Button) findViewById(R.id.Button01); 
      b.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,IMAGE_PICK); 
       } 
      }); 
     } 

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


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

        //MEDIA GALLERY 
        selectedImagePath = getPath(selectedImageUri); 




        if(selectedImagePath!=null) 
        { 
         path = selectedImageUri.toString(); 
         m1.setPath(path); 


          BitmapFactory.Options options = new BitmapFactory.Options(); 
          options.inSampleSize = 4; 

          Bitmap yourSelectedImage = BitmapFactory.decodeFile(path, options); 
          ImageButton img2=(ImageButton)findViewById(R.id.widget27); 
          img2.setImageBitmap(yourSelectedImage); 
          Toast.makeText(getBaseContext(), path, 1000).show(); 

          } 



        } 
      } 
     } 
     public String getPath(Uri uri) { 





     String [] proj={MediaStore.Images.Media.DATA}; 
     Cursor cursor = managedQuery(uri, 
       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(); 

     return cursor.getString(column_index); 

}}

请帮助在此先感谢

+0

你能PLS eleborate乌尔阙?你会得到错误和西隧错误WHR你? – 2011-02-01 06:11:19

回答

6

这段代码应放在你的onActivityResult。它可以让你从获取图像的URI解码文件路径的方式:

  Uri selectedImageUri = data.getData(); 
      String[] projection = { MediaStore.Images.Media.DATA}; 
      Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null); 
      int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      selectedImagePath = cursor.getString(column_index_data); 
      Bitmap galleryImage = BitmapFactory.decodeFile(selectedImagePath); 
+0

非常感谢你的工作 – kanchan 2011-02-02 05:30:03

0

我们需要做如下更改/在我们前面的onActivityResult()图库选择器代码修复对奇巧和所有其他早期无缝运行版本也是如此。

Uri selectedImgFileUri = data.getData();

如果(selectedImgFileUri == NULL){

// user has not selected any photo 

}

尝试{

的InputStream输入= mActivity.getContentResolver()openInputStream(selectedImgFileUri)。

mSelectedPhotoBmp = BitmapFactory.decodeStream(input);

}赶上(Throwable的TR){

//显示消息再次尝试

}

相关问题