2016-03-24 9 views
0

私の活動では私はImageViewを持っています。ユーザーがそれをクリックすると、ユーザーがアプリケーションを選択してそのアプリケーションで画像を表示できるよりもイメージを開くことができるアプリケーションのリストを表示するダイアログ(インテントダイアログなど)が開きます。Android:他のアプリとドローイング可能なリソースを共有する

私のアクティビティコード: - あなたが意図にビットマップを渡すことはできません

  Intent intent = new Intent(); 
      intent.setAction(Intent.ACTION_VIEW); 
      intent.setDataAndType(<your_image_uri>, "image/*"); 
      startActivity(intent); 

答えて

2

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ImageView iv = (ImageView) findViewById(R.id.imageid); 
    iv.setImageResource(R.drawable.dish); 
    iv.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //here is where I want a dialog that I mentioned show 
       } 
    }); 
}// end onCreate() 
-1

あなたがタイプIntent.ACTION_VIEWの意図を使用してstartActivityする必要があります。

私はあなたのリソースからdrawableを共有したいと思っています。だから最初にdrawableをビットマップに変換する必要があります。そして、ビットマップをファイルとして外部メモリに保存し、Uri.fromFile(new File(pathToTheSavedPicture))を使用してそのファイルのURIを取得し、そのURIをこのような意図に渡す必要があります。

shareDrawable(this, R.drawable.dish, "myfilename"); 

public void shareDrawable(Context context,int resourceId,String fileName) { 
    try { 
     //convert drawable resource to bitmap 
     Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId); 

     //save bitmap to app cache folder 
     File outputFile = new File(context.getCacheDir(), fileName + ".png"); 
     FileOutputStream outPutStream = new FileOutputStream(outputFile); 
     bitmap.compress(CompressFormat.PNG, 100, outPutStream); 
     outPutStream.flush(); 
     outPutStream.close(); 
     outputFile.setReadable(true, false); 

     //share file 
     Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(outputFile)); 
     shareIntent.setType("image/png"); 
     context.startActivity(shareIntent); 
    } 
    catch (Exception e) { Toast.makeText(context, "error", Toast.LENGTH_LONG).show(); 
    } 
} 
-1
Create a chooser by using the following code. You can add it in the part where you say imageview.setonclicklistener(). 
Intent intent = new Intent(); 
// Show only images, no videos or anything else 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
// Always show the chooser (if there are multiple options available) 
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1); 
関連する問題