2015-09-26 75 views
0

Im使用網格視圖來表示所有的圖像/照片,用戶選擇ImageView活動後執行,然後下面的代碼是共享該選定的圖像。 林有點混淆,我想我缺少的東西,因此URI顯示空值。 here是必要的整個代碼!無法使用意圖共享圖像 - URI null

public class Full_image extends ActionBarActivity { 

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

    // get intent data 
    Intent i = getIntent(); 

    // Selected image id 
    int position = i.getExtras().getInt("id"); 
    ImageAdapt imageAdapter = new ImageAdapt(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.menu_main, menu); 
    return true; 
} 


@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_share) { 
     return true; 
    } 

    // Handle item selection 
    ImageView image = (ImageView) findViewById(R.id.full_image_view); 
    Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); //file to be saved as per user selection 
    File sd = Environment.getExternalStorageDirectory(); 
    String fileName = "test.png"; //saved as png file 
    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.action_share: 
      Uri uri = Uri.fromFile(dest); 
      Intent shareIntent = new Intent(); 
      shareIntent.setAction(Intent.ACTION_SEND); 
      shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
      shareIntent.setType("image/png"); 
      startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.action_share))); //shared via Intent 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

} 

回答

0

我認爲這段代碼會爲你工作,你正在創建沒有寫權限的測試文件,我們需要告訴明確,使其可寫剩下的就是一樣的。

package com.desimeme.jai.desimeme;

import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.drawable.BitmapDrawable; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ImageView; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 


public class Full_image extends ActionBarActivity { 

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

     // get intent data 
     Intent i = getIntent(); 

     // Selected image id 
     int position = i.getExtras().getInt("id"); 
     ImageAdapt imageAdapter = new ImageAdapt(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.menu_main, menu); 
     return true; 
    } 

    public boolean isExternalStorageWritable() { 
     String state = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      return true; 
     } 
     return false; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
//  if (id == R.id.action_share) { 
//   return true; 
//  } 
     switch (item.getItemId()) { 
      case R.id.action_share: 
       // Handle item selection 
       ImageView image = (ImageView) findViewById(R.id.full_image_view); 
       Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); //file to be saved as per user selection 
       if (isExternalStorageWritable()) { 
        String sd = Environment.getExternalStorageDirectory().getAbsolutePath(); 
        File ssd = new File(sd, "share"); 
        if (!ssd.exists()) 
         ssd.mkdirs(); 
        String fileName = "test"; //saved as png file 
        File dest = new File(ssd, fileName + ".png"); 
        FileOutputStream out = null; 
        if (dest.canWrite()) { 
         dest.setWritable(true); 
        } 
         try { 
          out = new FileOutputStream(dest); 
          bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance 
          // PNG is a lossless format, the compression factor (100) is ignored 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } finally { 
          try { 
           if (out != null) { 
            out.close(); 
           } 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 

        Uri uri = Uri.fromFile(dest); 
        Intent shareIntent = new Intent(); 
        shareIntent.setAction(Intent.ACTION_SEND); 
        shareIntent.setType("*/*"); 
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
        startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.action_share))); //shared via Intent 
       } 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

} 
+0

它的工作,但現在它的共享空白圖像 –

+0

如果試圖通過Gmail電子郵件客戶端共享,那麼你需要通過編寫內容流文件提供和共享文件 – dex