活动从SDCard中选择图像后重新启动onActivityResult()

时间:2013-10-14 10:53:25

标签: android android-intent

我有一个应用程序,我从图库中选择一个图像,然后返回图像以在图像视图上设置它。问题是,当我设置第一张图像,并再次使用相同的按钮选择下一张图像时,第一张图像消失,当前图像被设置为第一张图像。我有一个线性布局,我在选择图像时动态创建图像视图。

下面是我的课程,在那里我选择按钮上的图像点击btnSelectPhotosGallery,然后onActivityResult就在那里。

@Override
public void onClick(View v)
{
    int id = v.getId();
    Intent i;
    switch (id)
    {
            //image selection on this button click
    case R.id.btnSelectPhotosGallery:
        Intent intent = new Intent();

        intent.setType("image/*");
        intent.setAction(Intent.ACTION_PICK);
        startActivityForResult(Intent.createChooser(intent,
                "Select Picture"), SELECT_PICTURE);
        break;

    default:
        break;
    }


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (resultCode == RESULT_OK)
    {
        if (requestCode == SELECT_PICTURE)
        {
            Uri selectedImageUri = data.getData();
            String imagePath = CommonMethods.getPath(ArticleForm2.this, selectedImageUri);
            File image = new File(imagePath);
            long sizeOfImage = ((image.length()/1024)/1024);
            if(sizeOfImage > 1.0)
            {
                Toast.makeText(ArticleForm2.this, "Image exceeding the Maximum Size(1 MB)", Toast.LENGTH_SHORT).show();
                //coverPhotoSelected = false;
            }
            else
            {
                //imageCoverPhoto.setImageURI(selectedImageUri);
                Bitmap bmp = getPreview(imagePath);
                ImageView img = new ImageView(ArticleForm2.this);
                img.setLayoutParams(new LayoutParams(300, 300));
                img.setImageBitmap(bmp);

                linearImages.addView(img);

            }
        }
    }
}


public Bitmap getPreview(String fileName) {
    File image = new File(fileName);

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image.getPath(), bounds);
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
        return null;
    }
    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
        : bounds.outWidth;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / 64;
    return BitmapFactory.decodeFile(image.getPath());
}

2 个答案:

答案 0 :(得分:0)

你可以尝试

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
OnClick-Method中的

。 (使用此功能启动的活动将在用户离开时立即完成)

答案 1 :(得分:0)

由于启动“相机活动”而导致方向更改。在您的Manifest中更改以下代码以调用Activity。

<activity
    android:name=".YourActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:screenOrientation="portrait" >
</activity>