【发布时间】:2015-09-15 11:17:09
【问题描述】:
在下面的代码中,我从 sd card 获取图像,并通过意图发送图像, 但是我想更改代码以发送图像的路径,而不是图像本身
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
//file name
filePath = data.getData();
try {
// Bundle extras2 = data.getExtras();
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte imageInByte[] = stream.toByteArray();
Intent i = new Intent(this, AddImage.class);
i.putExtra("image", imageInByte);
startActivity(i);
} catch (IOException e) {
e.printStackTrace(); } } }
and here I am receiving the image
byte[] byteArray = getIntent().getByteArrayExtra("image");
// encodedImage = Base64.encodeToString(byteArray, Base64);
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView imageview = (ImageView) findViewById(R.id.imageView);
imageview.setImageBitmap(bmp);
我也试过了,但没用:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
Intent i = new Intent(this, AddImage.class);
i.putExtra("imagepath", filePath);
startActivity(i);
} catch (IOException e) {
e.printStackTrace(); } }}
但是如何接收uri路径
geturi().getintent() ?
然后获取图片
编辑--
我在这里收到错误nullpointerexception uri string
img.setImageURI(Uri.parse(imagePath ));
这是代码
String imagePath = getIntent().getStringExtra("imagePath");
ImageView img=(ImageView) findViewById(R.id.imageView);
img.setImageURI(Uri.parse(imagePath ));
Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false);
// bitmap is the image
ByteArrayOutputStream stream = new ByteArrayOutputStream();
out.compress(Bitmap.CompressFormat.JPEG, 60, stream);
out.recycle();
/* byte[] byteArray = getIntent().getByteArrayExtra("image");
// encodedImage = Base64.encodeToString(byteArray, Base64);
// bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);*/
// ImageView imageview = (ImageView) findViewById(R.id.imageView);
img.setImageBitmap(out);
我正在发送这样的意图
filePath = data.getData(); 意图 i = 新意图(这个, AddImage.class); i.putExtra("imagepath", filePath); 开始活动(i);
【问题讨论】:
-
最好以字符串形式发送路径或保存在共享首选项中
-
@quicklearner 所以你是说从 onactivityresult 获取路径,将其保存在 sharedpreference 中,然后将其带到另一个活动?
-
在第一个活动中保存,然后在下一个活动中获取
-
String uri = getIntent().getStringExtra("imagePath");
-
@quicklearner 和 arun,你能给我看一个简单的例子吗?
标签: android