从网格视图中选择图像并在下一个活动中显示它,在此活动中,有一些按钮可以捕获图像并保存图像

时间:2015-09-28 06:18:53

标签: android android-intent android-activity gridview

我正在使用gridview显示图像。 选择图像后,它将显示在新活动中。 在新活动中,有2个按钮用于捕获图像,1个用于保存图像以及从gridview中选择的图像。 我的问题是,当我从gridview中选择图像时,下一个活动中的按钮不响应。当它们响应时,未显示从gridview中选择的图像。 捕获图像和保存按钮逻辑属于不同的类。 我试图通过意图调用这些。

这是我的代码问题所在,当我使用两个意图时,也只是工作。

          public class SingleItemView extends Activity {
           //final static int CAMERA_RESULT = 0;
          ImageView imv;    private int TAKENPHOTO = 0;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    // Get the view from singleitemview.xml
    setContentView(R.layout.singleitemview);


    // Get position from intent passed from MainActivity.java
    Intent i = getIntent();
    //startActivityForResult(i, 1);

    int position = i.getExtras().getInt("id");

    // Open the Image adapter
    ImageAdapter imageAdapter = new ImageAdapter(this);


    // Locate the ImageView in singleitemview.xml
    ImageView imageView = (ImageView) findViewById(R.id.image);

    // Get image and position from ImageAdapter.java and set into ImageView

    imageView.setImageResource(imageAdapter.mThumbIds[position]);
    //i = new Intent(SingleItemView.this, saveandshare.class);
//  startActivityForResult(i, 1);
    //Intent openNewActivity = new Intent(getApplicationContext(), saveandshare.class);
//  startActivity(openNewActivity);



}




    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (resultCode == RESULT_OK) {
            Bundle extras = intent.getExtras();
            Bitmap bmp = (Bitmap) extras.get("data");
        imv = (ImageView) findViewById(R.id.imageView);
    imv.setImageBitmap(bmp);




    }


}

1 个答案:

答案 0 :(得分:0)

您好请尝试下面的代码,希望它能帮助您

将下面的代码写入gridView的项目点击监听器

Intent intent = new Intent(m_context, SecondActivity.class);
intent.putExtra("ImagePath", "Your Image path(either it is url or uri)");
startActivity(intent);

在第二个活动中写下以下代码

if(getIntent().getExtras() != null)
{
    String path;
    path = getIntent().getExtras().getString("ImagePath");
}

在按钮中输入以下代码,以便从库中选择图像 并使用Universalimageloader库来加载图像

Intent m_galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(m_galleryIntent, 200);



  @Override
    protected void onActivityResult(int p_requestCode, int p_resultCode, Intent p_data)
    {
        if(p_requestCode == 200 && p_resultCode == RESULT_OK)
        {
            try
            {
                Uri m_selectedImageUri = p_data.getData();
                m_userSelectedImagePath = getPath(m_selectedImageUri);
                if(m_userSelectedImagePath.toString().startsWith("http"))
                {
                    m_loader.displayImage(m_userSelectedImagePath, m_ivScenarioImage);
                }
                else
                {
                    m_loader.displayImage("file://" + m_userSelectedImagePath, m_ivScenarioImage, m_options);
                }

            }
            catch(Exception m_e)
            {

            }
        }
    }

将以下功能用于商店媒体

private File getOutputMediaFile()
    {

        File m_mediaFile;

            m_builder = new StringBuilder();
            m_builder.append(Constant.m_mediaDirectoryPath).append(Constant.IMAGE_DIRECTORY_NAME);
            File m_imageStorageFile = new File(m_builder.toString());

            // Create the storage directory if it does not exist
            if(! m_imageStorageFile.exists())
            {
                if(! m_imageStorageFile.mkdirs())
                {
                    return null;
                }
            }
            // Create a media file name
            String m_timeStamp = new SimpleDateFormat(Constant.TIME_STAMP_FORMAT, Locale.getDefault()).format(new java.util.Date());
            m_builder = new StringBuilder();
            m_builder.append(m_imageStorageFile.getPath()).append(File.separator).append("IMG_").append(m_timeStamp).append(".jpg");
            m_mediaFile = new File(m_builder.toString());


        return m_mediaFile;
    }

在上述功能之后,您必须在保存图像按钮中编写以下代码,然后单击

try
    {
       String saveImagePath;
        saveImagePath = getOutputMediaFile().getPath();
        m_bitmap = CommonUtills.getImageBitmap(m_context, m_userSelectedImagePath, 500, 500);
        ByteArrayOutputStream m_out = new ByteArrayOutputStream();
        if(m_bitmap != null)
        {
                 m_ivScenarioImage.setImageBitmap(m_bitmap);
        }
    }
    catch(Throwable m_e)
    {

    }


     public static Bitmap getImageBitmap(Context p_context,String p_filePath, int p_width, int p_height)
    {
        try
        {
            ExifInterface m_exif = new ExifInterface(p_filePath);
            int m_rotation = m_exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

            Matrix m_localMatrix = new Matrix();

            if(m_rotation == ExifInterface.ORIENTATION_ROTATE_90)
                m_localMatrix.postRotate(90);
            else if(m_rotation == ExifInterface.ORIENTATION_ROTATE_180)
                m_localMatrix.postRotate(180);
            else if(m_rotation == ExifInterface.ORIENTATION_ROTATE_270)
                m_localMatrix.postRotate(270);
            else
                m_localMatrix.postRotate(0);

         //   Bitmap  m_retBmp = decodeFile(p_context, p_filePath,p_width,p_height);
           // Bitmap m_retBmp = ImageHelper.scaleImage(p_filePath, p_width, p_height);
          //  Bitmap m_retBmp =  CommonUtills.decodeFile(p_context, p_filePath, p_width, p_height);
            Bitmap m_retBmp =  CommonUtills.scaleImage(p_context,p_filePath,p_width,p_height);

            return Bitmap.createBitmap(m_retBmp, 0, 0, m_retBmp.getWidth(), m_retBmp.getHeight(), m_localMatrix, true);
        }
        catch(Throwable p_e)
        {

        }
        return null;
    }



     public static Bitmap scaleImage(Context p_context, String p_path, int p_reqHeight, int p_reqWidth)
    {
        Bitmap m_retBmp = null;
        try
        {
            System.gc();
            File m_file = new File(p_path);

            if (m_file.exists())
            {
                BitmapFactory.Options m_o2 = new BitmapFactory.Options();
                m_o2.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(m_file.getPath(), m_o2);

                m_o2.inSampleSize = calculateInSampleSize(m_o2, p_reqHeight, p_reqWidth);

                m_o2.inJustDecodeBounds = false;
                m_retBmp = BitmapFactory.decodeFile(m_file.getPath(), m_o2);
            }
        }
        catch (Exception p_e)
        {

        }

        return m_retBmp;
    }


     private static int calculateInSampleSize(BitmapFactory.Options p_options, int p_reqWidth, int p_reqHeight)
    {
        // Raw height and width of image
        final int m_height = p_options.outHeight;
        final int m_width = p_options.outWidth;
        int m_inSampleSize = 1;

        if (m_height > p_reqHeight || m_width > p_reqWidth)
        {

            final int m_halfHeight = m_height / 2;
            final int m_halfWidth = m_width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((m_halfHeight / m_inSampleSize) > p_reqHeight && (m_halfWidth / m_inSampleSize) > p_reqWidth)
            {
                m_inSampleSize *= 2;
            }
        }
        return m_inSampleSize;
    }