我需要使用该图像的路径将图像从图库发送到另一个活动,但没有发生任何事情...... 帮帮我...... 这是代码..
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);
}
}
和第二个活动代码是
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));
}
答案 0 :(得分:2)
intent.putExtra("imagePath",filePathColon );
应该是
intent.putExtra("imagePath",picturepath);
因为filepathcolon引用列索引而picturepath引用URI
并使用
获取String ps=getIntent().getStringExtra("imagePath"); //it should be same as you send it
答案 1 :(得分:2)
请改变这样
Intent intent= new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("imagePath",picturepath);
startActivity(intent);
}
答案 2 :(得分:1)
您的Intent中的两个字符串不匹配: 您在第一个Activity中编写了带有大写P的imagePath,但在第二个Activity中没有。
String ps=getIntent().getStringExtra("imagePath");
修复它