Androidで他のIntent呼び出し時に画像を送ります。
※暗黙的Intent呼び出しの場合。
【手順1】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの作成手順で、「AndroidSample11-002-IntentActionSendImage」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
| 項目名 | 項目に設定する値 |
| アプリケーション名(Application Name) | com.example.androidsample11_002 |
| プロジェクト名(Project Name) | AndroidSample11-002-IntentActionSendImage |
| パッケージ名(Package Name) | com.example.androidsample11_002 |
| Build SDK | API 8 |
| Minimum Required SDK | API 8 |
【手順2】
1.「AndroidManifest.xml」は以下の通り。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidsample11_002"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
????
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidsample11_002.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順3】
1.「任意のディレクトリの作成方法」の手順で「res/drawable」ディレクトリを作成。
2.「画像」を「res/drawable」に「android_sample_image_100_100.png」という名前で保存。
【手順4】
1.「res/layout/activity_main.xml」は以下の通り。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android_sample_image_100_100" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickButton"
android:text="ボタン" />
</LinearLayout>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順5】
1.「src/com/example/androidsample11_002/MainActivity.java」は以下の通り。
package com.example.androidsample11_002;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickButton(View viwe) {
ImageView imageView = (ImageView) findViewById(R.id.imageView);
try {
BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
Bitmap bmp = bd.getBitmap();
String sdDir = Environment.getExternalStorageDirectory().getPath();
String packageName = getPackageName();
File dir = new File(sdDir + File.separator + packageName);
dir.mkdirs();
String fileName = dir.getAbsolutePath() + File.separator + System.currentTimeMillis() + ".png";
FileOutputStream out = new FileOutputStream(fileName);
bmp.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "タイトル");
shareIntent.putExtra(Intent.EXTRA_TEXT, "テキスト");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(fileName)));
shareIntent.setType("image/png");
startActivity(shareIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順6】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの実行手順で、実行。
【手順7】
1.以下の様に表示されれば成功です。
以上です。



0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。