2014-02-22 45 views
0

如何使用用戶選擇的特定ID替換R.id.ic_launcher 我已經創建了一個應用程序,它代表圖像的一些網格視圖,然後在用戶選擇之後選擇特定圖像,然後共享菜單按鈕將圖像共享到社交應用程序。 但錯誤是每當選擇任何圖像並將其共享給任何應用程序時,它只發送默認啓動器圖標,即ic_launcher.png。 我的代碼低於這樣的: FullImageActivity.java僅在Android中共享選定圖像

@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而不是選擇的圖像。 進一步我保留.jpeg格式的所有圖像在ImageAdapter.java類。

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> 

告訴我在哪裏需要修改我的代碼以正確工作,如上所述。

回答

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

這是部分你在哪裏發射的圖像保存到文件,因此共享該圖像

+0

我該怎麼辦這個部分? –

+0

初學者看看本教程[教程](http://www.mkyong.com/android/android-gridview-example/) – user3340677

+0

BitmapFactory.decodeResource(getResources(),imageAdapter.mThumbIds [position]); – user3340677