从应用程序文件夹共享图像

时间:2017-10-14 09:15:04

标签: java android

我希望在click

button上分享图片

Java代码

public void share(View v)
{
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    Uri a=Uri.parse("android.resource://"+getPackageName()+"/"+R.drawable.pic); 
    Log.i("imageUri",""+imageUri);
    share.putExtra(Intent.EXTRA_STREAM,a);
    startActivity(Intent.createChooser(share,"Share Image"));
}

这段代码不起作用,我应该做些什么改变?

2 个答案:

答案 0 :(得分:1)

试试这个,

首先您需要添加权限 WRITE_EXTERNAL_STORAGE

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

注意:适用于Marshmallow及以上版本您需要 WRITE_EXTERNAL_STORAGE Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.pic); Intent share = new Intent(Intent.ACTION_SEND); share.setType("image/jpeg"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); b.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = MediaStore.Images.Media.insertImage(getContentResolver(), b, "Title", null); Uri a= Uri.parse(path); share.putExtra(Intent.EXTRA_STREAM, a); startActivity(Intent.createChooser(share, "Select")); Runtime Permission

的好例子

然后使用以下代码分享您的图片

CREATE TABLE Compositions(
CompositionId CHAR(6) NOT NULL,
Composer VARCHAR(25) NOT NULL,
Name VARCHAR(30) NOT NULL,
YearWritten VARCHAR(4) NOT NULL,
PerformerId CHAR(6) NULL,
PerformDate DATETIME NULL,
CONSTRAINT pk_compositions PRIMARY KEY (CompositionId)
);

CREATE TABLE CompositionCollection(
ConcertId CHAR(6) NOT NULL,
CompositionId INT NOT NULL,
PerformerId CHAR(6) NOT NULL,
PerformTime TIME NOT NULL,
PerformDate DATE NOT NULL,
CONSTRAINT pk_pTime PRIMARY KEY (PerformTime)
);


-- compositions data values
INSERT INTO Compositions VALUES 
('CC01', 'Alexander Borodin', 'Scherzo in a 
Flat', '1883', '', '2017-09-17'),
('CC02', 'Alexander Borodin', 'Symphony No 
2', '1869', '', ''),
('SC01', 'Beethoven', 'Symphony No 6', 
'1802', '', ''),
('SC02', 'Edward Elgar', 'Cello Concerto', 
'1997', '', '');

-- composition collection data values
-- All the values below is for one concert and they are all played at the 
-- same date
INSERT INTO CompositionCollection VALUES 
('123456', 'CC01', 'C012', 
'08:00:00', '2017-09-17'),
('123456', 'CC02', 'C012', 
'08:15:00', '2017-09-17'),
('123456', 'SC01', 'S512', 
'08:20:00', '2017-09-17'),
('123456', 'SC02', 'C012', 
'08:40:00', '2017-09-17');

答案 1 :(得分:0)

使用此代码分享图片:

注意:您必须在WRITE/READ EXTERNAL STORAGE文件中添加Menifest权限才能执行此操作:

           Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
            String path = MediaStore.Images.Media.insertImage(getContentResolver(),
                    mBitmap, "Image Description", null);
            Uri uri = Uri.parse(path);

            Intent intent = new Intent(Intent.ACTION_SEND);
            Log.d("imageUri", "imageUriIs" + uri);
            intent.putExtra(Intent.EXTRA_STREAM, uri);
            intent.putExtra(Intent.EXTRA_SUBJECT, "");
            intent.putExtra(Intent.EXTRA_TEXT, shareMSG);
            intent.putExtra(Intent.EXTRA_TITLE, "TITLE");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setType("image/*");
            startActivity(Intent.createChooser(intent, "APPNAME"));