Share the Selected Image Only in Android

2019-04-17 10:22发布

HOW CAN I REPLACE THE R.id.ic_launcher with the specific id that user selected. i have created an application which represents some grid view of images, then after user selection it select the specific image and then a share menu button share the image to the social apps. but the error is whenever any image is selected and shared to any application it only sends the default launcher icon i.e, ic_launcher.png. my code goes below like this: FullImageActivity.java

@SuppressLint("SdCardPath")
public class FullImageActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);

    Intent i = getIntent();

    int position = i.getExtras().getInt("id");
    ImageAdapter imageAdapter = new ImageAdapter(this);

    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageResource(imageAdapter.mThumbIds[position]);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    File sd = Environment.getExternalStorageDirectory();
    String fileName = "test.png";
    File dest = new File(sd, fileName);
    try {
        FileOutputStream out;
        out = new FileOutputStream(dest);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    switch (item.getItemId()) {
        case R.id.item:
            Uri uri = Uri.fromFile(dest);
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            shareIntent.setType("image/jpeg");
            startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

} this is where i implemented the share method, this is shere my code sending the defautl ic_launcher.png instead of selected image. further i kept all the images in .jpeg format in ImageAdapter.java class.

private Context mContext;

// Keeping all Images in array
public Integer[] mThumbIds = {

        R.drawable.rage_0001,R.drawable.rage_0002,

and my AndroidManifest.XML goes like this for all uses permissions and activity records.

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:permission="android.permission.WRITE_EXTERNAL_STORAGE"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.jai.desimeme.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="image/*" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="text/plain" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND_MULTIPLE" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="image/*" />
        </intent-filter>
    </activity>
    <!-- FullImageActivity -->
    <activity android:name="com.jai.desimeme.FullImageActivity" >
    </activity>
    <activity
        android:name="com.jai.desimeme.About"
        android:theme="@android:style/Theme.Dialog" >
    </activity>
    <activity
        android:name="com.jai.desimeme.ShareActivity"
        android:label="@string/title_activity_share" >
    </activity>
</application>

tell me where i need to modify my code for proper working as i mentioned above.

1条回答
ゆ 、 Hurt°
2楼-- · 2019-04-17 10:40
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),drawable.ic_launcher);//launcher  being saved to file
File sd = Environment.getExternalStorageDirectory();
String fileName = "test.png";
File dest = new File(sd, fileName);
try {
    FileOutputStream out;
    out = new FileOutputStream(dest);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    out.flush();
    out.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

This is the part where you are saving the launcher image to a file and hence sharing that image

查看更多
登录 后发表回答