在图像被选中之后,下面的代码中的每一个都没有移动到下一个活动,如果我们选择它保留在同一活动中的第一个项目但是我们选择除了第一个位置图像之外的另一个项目到下一个活动
startActivity(mv);
当我们点击第一张位置图片
但吐司正在显示,因为图片已被选中,但未移至下一个活动
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
case R.id.imageView2:
upLoadPhoto();
break;
protected void upLoadPhoto() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("return-data", true);
startActivityForResult(intent, 100);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && data != null && data.getData() != null) {
System.out.println("in case");
Uri _uri = data.getData();
if (_uri != null) {
// User had pick an image.
Cursor cursor = getContentResolver()
.query(_uri,
new String[] { android.provider.MediaStore.Images.ImageColumns.DATA },
null, null, null);
cursor.moveToFirst();
// Link to the image
final String imageFilePath = cursor.getString(0);
Log.v("imageFilePath", imageFilePath);
File photos = new File(imageFilePath);
try {
gbmp = BitmapFactory.decodeStream(
new FileInputStream(photos), null, null);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cursor.close();
}
mv = new Intent(Imageselection.this, Modeselection.class);
mv.putExtra("test", gbmp);
mv.putExtra("name", 100);
System.out.println("going to gamestart class");
startActivity(mv);
Toast.makeText(getApplicationContext(), "Image selected", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:3)
因为你将整个图像传递给了包。
捆绑包的大小有限,你无法将图像本身放入意图中。
您需要将图像保存到缓存中,然后将图像的文件名或文件路径传递给putExtra
,然后通过访问文件名或文件路径来检索它。
对于您的情况,您从图库中选择一个图像,然后您可以获取该图像的URI或路径,将URI /路径放到intent中,然后在另一个活动中检索它。
当您调用意图启动图库时,它将返回data
,其中包含所选文件的Uri。
这里是一些示例代码,如果您启动默认库,则可能需要这些代码:
// Launch Gallery to choose pic.
Intent intentLaunchGallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intentLaunchGallery, LOAD_IMAGE_ACTIVITY_REQUEST_CODE);
...
private String getPath(Uri uri) {
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
...
// Gallery launched to choose picture
if (requestCode == LOAD_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
fileUri = data.getData();
filePath = getPath(fileUri);
// fileUri = Uri.parse(filePath);
// call media scanner to refresh gallery
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{filePath}, null, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
Log.i("MediaScanner", "Scanned " + path + ":");
Log.i("MediaScanner", "-> uri=" + uri);
}
});
// Toast.makeText(this, "Image chosen from: " + filePath, Toast.LENGTH_LONG).show();
Log.d("MainMenu->onActivityResult", "Image chosen from: " + filePath);
// display the picture chosen by user
Intent intentShowMarkers = new Intent(MainMenuActivity.this, ShowMarkersActivity.class);
intentShowMarkers.putExtra("IMG", filePath);
intentShowMarkers.putExtra("FLAG", false);
MainMenuActivity.this.startActivity(intentShowMarkers);
} else if (resultCode == RESULT_CANCELED) {
// user pressed the cancel of gallery
Toast.makeText(MainMenuActivity.this, "Cancelled.", Toast.LENGTH_SHORT).show();
}
}
答案 1 :(得分:0)
尝试以下代码。从gallary选择图像
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
并在您的onActivityResult
方法中写下以下意图去参加下一个活动
Intent picIntent = new Intent(CurrentActivity.this,
NextActivity.class);
picIntent.putExtra("gallery", filePath);
startActivity(picIntent);
在您的NextActivity
类onCreate
方法编写下面的代码
ImageView imageView = (ImageView)findViewById(R.id.imgView);
String fileString = getIntent().getStringExtra("gallery");
imageView.setImageBitmap(BitmapFactory.decodeFile(fileString));