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);
}
}
}
它的工作,但現在它的共享空白圖像 –
如果試圖通過Gmail電子郵件客戶端共享,那麼你需要通過編寫內容流文件提供和共享文件 – dex