从图库fc附加图像,重复附加相同的图像

时间:2011-09-05 08:53:59

标签: android

我附加了来自画廊的图像并显示其他视图我附加了2次成功完整但是在下一次附加图片来自画廊部队的试用关闭我的应用程序,为什么?我很惊讶它为什么会发生?

开放式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);
它附加了两次或一次,但是在接下来的一次接下来的试验中它会强行关闭...

提前致谢

1 个答案:

答案 0 :(得分:0)

i使用此代码从图库中选择图像....

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);

}