Share images from gallery in Android

前提是你 提交于 2019-12-13 00:14:20

问题


i have written a program which has two buttons , one is for gallery which when clicked displays my images from gallery and another button which starts my camera which displays the captured images.

The images are getting displayed properly , but I am not able to understand how to share the displayed images to other apps , please need some help.

Below here is my code for sharing : I have put question mark because i need to know what i should pass inside my Uri so that i can get the path of my images

imgShare.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            intent.setType("image/*");
            Uri selectedImage = ????
            intent.putExtra(Intent.EXTRA_STREAM, selectedImage);
            startActivityForResult(
                    Intent.createChooser(intent, "Select Picture"),
                    SELECT_PICTURE);


        }

My code for startActivityForResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK
            && null != data) {

        // Toast.makeText(getBaseContext(),
        // "File saved at " + imageFile.getAbsolutePath(),
        // Toast.LENGTH_SHORT).show();
        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]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        /*
         * Bitmap bitmap = BitmapFactory .decodeFile( Environment
         * .getExternalStoragePublicDirectory
         * (Environment.DIRECTORY_PICTURES) + File.separator + ".jpg",
         * options);
         */
        Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
        int newWidth = bitmap.getWidth();
        int newHeight = bitmap.getHeight();

        while (newWidth > 1200) {
            newWidth = newWidth / 2;
            newHeight = newHeight / 2;
        }
        img.setImageBitmap(Bitmap.createScaledBitmap(bitmap, newWidth,
                newHeight, false));

    }

any suggestions are welcomed , thanking you


回答1:


These are called intents, some application are listening for contents like images and plain text. when you call these intent, a dialog will open of those applications are listening for your content.

Share single Image

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

Share multiple Image

ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(imageUri1); // Add your image URIs here
imageUris.add(imageUri2);

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));

Get ImageURI

public Uri getImageUri(Context inContext, Bitmap inImage) {
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  return Uri.parse(path);
}

OR

Uri.parse(new File("/sdcard/yourImage.jpg").toString())

More you can here on Android official website



来源:https://stackoverflow.com/questions/27314877/share-images-from-gallery-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!