简体   繁体   中英

passing gallery image to another activity through intent

I need to send a image from gallery to another activity by using the path of that image but nothing happened... help me out...... here is the code..

        protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
             super.onActivityResult(requestCode, resultCode, data);
           if(requestCode==RESULT_LOADIMAGE&&resultCode==RESULT_OK&&null!=data)
       {
        Uri selectedImages=data.getData();
        String[] filePathColon={MediaStore.Images.Media.DATA};
        Cursor cursr=getContentResolver().query(selectedImages, filePathColon, null, null, null);
        cursr.moveToFirst();
        int columnindex=cursr.getColumnIndex(filePathColon[0]);
        String picturepath=cursr.getString(columnindex);
        cursr.close();

    Intent  intent= new Intent(MainActivity.this,SecondActivity.class);
      intent.putExtra("imagePath",filePathColon );
        startActivity(intent);

       }
   }

and second activity code is

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    img=(ImageView)findViewById(R.id.imageView1);


       getData();

       }

      private void getData(){
    String ps=getIntent().getStringExtra("imagepath");

    img.setImageBitmap(BitmapFactory.decodeFile(ps));

}   
intent.putExtra("imagePath",filePathColon );

should be

intent.putExtra("imagePath",picturepath);

because filepathcolon refers to column index and picturepath refers to the URI

and get it using

String ps=getIntent().getStringExtra("imagePath"); //it should be same as you send it

please change like this

      Intent  intent= new Intent(MainActivity.this,SecondActivity.class);
  intent.putExtra("imagePath",picturepath);
    startActivity(intent);

   }

The two strings in your Intent do not match: You wrote imagePath with capital P in the first Activity, but not in your second one.

String ps=getIntent().getStringExtra("imagePath");

fixes it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM