仅在Android中共享所选图像

时间:2014-02-22 14:56:26

标签: java android android-intent share

我如何使用用户选择的特定ID替换R.id.ic_launcher 我创建了一个代表图像网格视图的应用程序,然后在用户选择后选择特定图像,然后共享菜单按钮将图像共享给社交应用程序。 但错误是每当选择任何图像并将其共享给任何应用程序时,它只发送默认的启动器图标,即ic_launcher.png。 我的代码如下所示: 的 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);
    }
}

} 这是我实现共享方法的地方,这是我的代码发送defautl ic_launcher.png而不是选择的图像。 此外,我将所有图像保存为{strong> ImageAdapter.java 类中的.jpeg格式。

private Context mContext;

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

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

和我的 AndroidManifest.XML 对于所有使用权限和活动记录都是这样的。

<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>

告诉我在哪里需要修改我的代码以便正常工作,如上所述。

1 个答案:

答案 0 :(得分:0)

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();
}

这是将启动器图像保存到文件并因此共享该图像的部分