Android 从网格视图中选择图像并在下一个活动中显示,在该活动中有用于捕获图像和保存图像的按钮

Android 从网格视图中选择图像并在下一个活动中显示,在该活动中有用于捕获图像和保存图像的按钮,android,android-intent,android-activity,gridview,Android,Android Intent,Android Activity,Gridview,我正在显示来自gridview的图像。 选择图像后,它将显示在“新建活动”中。 在“新活动”中有两个按钮,一个用于捕获图像,另一个用于将其与从gridview中选择的图像一起保存。 我的问题是,当我从gridview中选择图像时,下一个活动中的按钮不响应。当它们响应时,则不显示从gridview中选择的图像。 捕获图像和保存按钮逻辑在不同的类中。 我正试图通过故意来称呼这些人 这是我的代码,问题是,当我使用两个意图时,也只是在工作 public class SingleIte

我正在显示来自gridview的图像。 选择图像后,它将显示在“新建活动”中。 在“新活动”中有两个按钮,一个用于捕获图像,另一个用于将其与从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);




    }


}

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

将以下代码写入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");
}
在你的按钮中写下下面的代码,点击从图库中选择图像 并使用库加载图像

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

仍在查看捕获图像,而不是从galley中选择的图像。我想查看这两个图像。我还向您发送了商店捕获图像的代码。使用getOutputMediaFile函数。工作正常