sharing image from drawable

青春壹個敷衍的年華 提交于 2019-12-08 13:14:57

问题


I want create activity for share image from drawable source,

and this is my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Uri imageUri = Uri.parse("android.resource://com.androidbegin.shareimagetutorial/drawable"+R.drawable.ic_launcher);
              Intent intent = new Intent(Intent.ACTION_SEND);
              intent.setType("image/*");

              intent.putExtra(Intent.EXTRA_STREAM, imageUri);
              startActivity(Intent.createChooser(intent , "Share"));
        }
    });

and this is my manifest app:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidbegin.shareimagetutorial"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />


<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:uiOptions="splitActionBarWhenNarrow" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

but , when i launch app in nexts 7 ,and i select bluetooth ,

i get the unkown file not found toast message!!

can any body help what is this problem?!


回答1:


You want to share an image from drawable resource. But other apps have no access to your resources. There are two ways to solve this. 1. Use a content provider. 2. Copy the file to external/public memory first.




回答2:


Try this..

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("image/*");

    Uri uri = Uri.parse("android.resource://com.androidbegin.shareimagetutorial/"+R.drawable.ic_launcher);
    //Or
    Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.drawable.ic_launcher);

    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.putExtra(Intent.EXTRA_TEXT, "Share");
    startActivity(Intent.createChooser(shareIntent, "Share"));



回答3:


Try this......

Firstly get Image from Drawable to imageView and then do the followiing.....

this.imageView.buildDrawingCache();
             File var4 = new File(Environment.getExternalStorageDirectory().toString(), "Android/data/com.example.share;/cache/share_cache.jpg");
             var4.getParentFile().mkdirs();

             try {
                var4.createNewFile();
             } catch (Exception var18) {
                ;
             }

             FileOutputStream var8;
             label36: {
                FileOutputStream var7;
                try {
                   var7 = new FileOutputStream(var4);
                } catch (Exception var19) {
                   var8 = null;
                   break label36;
                }

                var8 = var7;
             }

             this.imageView.getDrawingCache();
             this.imageView.getDrawingCache().compress(CompressFormat.JPEG, 85, var8);

             try {
                var8.flush();
                var8.close();
             } catch (IOException var17) {
                ;
             }

             Intent var12 = new Intent("android.intent.action.SEND");
             var12.setType("image/jpeg");
             var12.putExtra("android.intent.extra.STREAM", Uri.parse("file://" + var4.getAbsolutePath()));
             this.startActivity(Intent.createChooser(var12, "Share Image"));


来源:https://stackoverflow.com/questions/24797299/sharing-image-from-drawable

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