2013-03-19 45 views
0

我在做這個主題的R & D.如何在android/ANDROID中將圖片從SD卡分享給Gmail?

我從圖庫中獲取圖像並能夠在圖像視圖中查看。

並通過長按圖像視圖我可以分享。

但問題是我沒有得到附加的圖像作爲輸出..

public class Facebookhome extends Activity { 

Button share; 

ImageView img; 

Uri screenshotUri; 

private static final int SELECT_PICTURE = 1; 

private String selectedImagePath; 

@Override 

protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_facebookhome); 

    share = (Button) findViewById(R.id.button1); 

    img = (ImageView) findViewById(R.id.imageView1); 

    share.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      Intent intent = new Intent(); 

      intent.setType("image/*"); 

      intent.setAction(Intent.ACTION_GET_CONTENT); 

      startActivityForResult(

        Intent.createChooser(intent, "Select 

Picture"), 

        SELECT_PICTURE); 

     } 

    }); 


    img.setOnLongClickListener(new View.OnLongClickListener() { 

     public boolean onLongClick(View v) { 

      shareimage(); 

      return true; 

     } 

    }); 

} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (resultCode == RESULT_OK) { 

     if (requestCode == SELECT_PICTURE) { 

      Uri selectedImageUri = data.getData(); 

      selectedImagePath = getPath(selectedImageUri); 

      System.out.println("Image Path : " + selectedImagePath); 

      img.setImageURI(selectedImageUri); 

     } 

    } 

} 

public String getPath(Uri uri) { 

    String[] projection = { MediaStore.Images.Media.DATA }; 

    Cursor cursor = managedQuery(uri, projection, null, null, null); 

    int column_index = cursor 

      .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 

    cursor.moveToFirst(); 

    return cursor.getString(column_index); 

} 

public void shareitem() { 

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 

    sharingIntent.setType("text/plain"); 

    String shareBody = "Here is the share content body"; 

    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 

      "Subject Here"); 

    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); 

    startActivity(Intent.createChooser(sharingIntent, "Share via")); 

} 

public void shareimage() { 

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 

    screenshotUri = Uri.parse(selectedImagePath); 

    sharingIntent.setType("image/jpg"); 

    sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, 

      screenshotUri); 

    startActivity(Intent.createChooser(sharingIntent, "Share image using")); 

    // Toast.makeText(getBaseContext(), "FB Last", 

    // Toast.LENGTH_LONG).show(); 

} 
} 
+0

什麼你問? 「但問題是我沒有得到所附的圖像作爲輸出」請詳細說明。 – JoxTraex 2013-03-19 09:22:32

+0

我從圖像查看圖像。長按那個圖像視圖我想把這個圖像附加到Gmail。但是我只是在附件裏找到了圖片名稱而不是內容。 – user2185718 2013-03-19 09:29:51

+0

我需要一個附件中的圖像,當我點擊分享我想分享選定的圖像作爲默認。請幫幫我 – user2185718 2013-03-19 10:01:55

回答

0
private static int RESULT_LOAD_IMAGE = 1; 


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

    Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture); 
    buttonLoadImage.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent i = new Intent(
        Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

      startActivityForResult(i, RESULT_LOAD_IMAGE); 
     } 
    }); 
} 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

     Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     Uri screenshotUri = Uri.parse(picturePath); 
     cursor.close(); 

     Intent i = new Intent(Intent.ACTION_SEND); 
     i.setType("image/jpg""); 
     i.putExtra(Intent.EXTRA_EMAIL, 
       new String[] { "[email protected]" }); 
     i.putExtra(Intent.EXTRA_SUBJECT, "subject of email"); 
     i.putExtra(Intent.EXTRA_STREAM, screenshotUri); 
     startActivity(Intent.createChooser(i, "Send mail...")); 


    } 


} 
+1

我終於得到了答案我自己public void shareimage(){ \t \t Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); \t \t screenshotUri = Uri.fromFile(new File(selectedImagePath)); \t \t sharingIntent.setType(「image/jpg」); \t \t sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, \t \t \t \t screenshotUri); (Intent.createChooser(sharingIntent,「Share image using」)); \t \t // Toast.makeText(getBaseContext(),「FB Last」, \t \t // Toast.LENGTH_LONG).show(); \t} – user2185718 2013-03-19 11:43:56