1

嗨,下面代码中的每个人,在选择图像后,它不会移动到下一个活动,在图库中,如果我们选择第一个项目,它仍然在同一个活动中,但我们选择第一个位置图像以外的另一个项目,它正在移动到下一个活动

startActivity(mv);当我们单击第一个位置图像时,显示的 startactvity 没有调用

但是吐司显示为已选择图像但未移动到下一个活动

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();
            }
4

2 回答 2

3

这是因为您将整个图像传递给了捆绑包。

捆绑包的大小有限,您不能将图像本身放入意图中。

您需要将图像保存到缓存中,然后将图像的文件名或文件路径传递给 putExtra,然后稍后通过访问文件名或文件路径来检索它。

对于您的情况,您从图库中选择一张图片,然后您可以获取该图片的 URI 或路径,将 URI/路径放入意图,然后在您的另一个活动中检索它。

当您调用意图启动图库时,它将返回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();

        }
    }
于 2012-10-02T11:28:42.937 回答
0

试试下面的代码。从图库中选择图像

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);

在你的NextActivityonCreate方法中写下面的代码

ImageView imageView = (ImageView)findViewById(R.id.imgView);

        String fileString = getIntent().getStringExtra("gallery");


        imageView.setImageBitmap(BitmapFactory.decodeFile(fileString));
于 2012-10-02T11:03:20.493 回答