我有一个活动,我在表面视图中打开相机并捕获图像。捕获显示在图像视图的下一个活动。但结果活动显示白色屏幕数据未传递到结果活动请告诉我我如何将图像传递给下一个活动?
public class MainActivity extends AppCompatActivity
{
int REQUEST_IMAGE_CAPTURE = 1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void takePicture(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE)
{
if (resultCode == RESULT_OK)
{
Intent i = new Intent(this,Result.class);
i.putExtra("filepath",1);
startActivity(i);
}
}
}
}
结果活动
public class Result extends Activity
{
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
iv = (ImageView)findViewById(R.id.imageView);
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("filepath");
iv.setImageBitmap(bmp);
}
}
答案 0 :(得分:0)
更改此i.putExtra("filepath",yourfilepath);
并在下一个活动中获取..
Intent intent=getIntent();
String filepath=intent.getStringExtra("filepath");
在位图中对其进行解码..
File sd = Environment.getExternalStorageDirectory();
File image = new File(sd+filePath, imageName);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);
另外你可以在这里获得imagepath。
在onActivityResult(...){
Bitmap photo = (Bitmap) data.getExtras().get("data");
Uri tempUri = getImageUri(getApplicationContext(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATH
String finalpath =getRealPathFromURI(tempUri)
//now you can putextra here
这是方法。
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}