从Android的画廊分享图片 [英] Share images from gallery in Android
问题描述
我已经写了有两个按钮,一个方案,一个是从画廊点击显示我的图片库,另一个按钮,开始我的摄像头,显示拍摄的图像。
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.
下面这里是我的code分享:我已经把问号,因为我需要知道我应该通过我的乌里里,这样我可以让我的图片的路径
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);
}
我的code为startActivityForResult
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
推荐答案
这些被称为意图,有些应用程序正在侦听如图像和纯文本内容。当你调用这些意图,将打开一个对话框这些应用程序都聆听您的内容。
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.
分享单一图片
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)));
分享多个图片
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.."));
获取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);
}
或
Uri.parse(new File("/sdcard/yourImage.jpg").toString())
更可以在这里 Android的官方网站
这篇关于从Android的画廊分享图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!