Skip to content

Instantly share code, notes, and snippets.

@ekos
Created November 1, 2012 12:05
Show Gist options
  • Save ekos/3993270 to your computer and use it in GitHub Desktop.
Save ekos/3993270 to your computer and use it in GitHub Desktop.
package com.yokogoshi.line_intent_test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
static final int REQUEST_ACTION_PICK = 1;
public static final String PACKAGE_NAME = "jp.naver.line.android";
public static final String CLASS_NAME = "jp.naver.line.android.activity.selectchat.SelectChatActivity";
private List<ApplicationInfo> m_appList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void galleryHandler(View view) {
if(checkLineInstalled()){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent,"select"), REQUEST_ACTION_PICK);
}else{
Toast toast = Toast.makeText(this, "LINEがインストールされていません", Toast.LENGTH_SHORT);
toast.show();
}
}
public void sendTextHandler(View view) {
String sendText = ((TextView)findViewById(R.id.send_text)).getText().toString();
if(checkLineInstalled()){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setClassName(PACKAGE_NAME, CLASS_NAME);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, sendText);
startActivity(intent);
}else{
Toast toast = Toast.makeText(this, "LINEがインストールされていません", Toast.LENGTH_SHORT);
toast.show();
}
}
private boolean checkLineInstalled(){
PackageManager pm = getPackageManager();
m_appList = pm.getInstalledApplications(0);
boolean lineInstallFlag = false;
for (ApplicationInfo ai : m_appList) {
if(ai.packageName.equals(PACKAGE_NAME)){
lineInstallFlag = true;
break;
}
}
return lineInstallFlag;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String fname = "send_image.jpg";
String fileFullPath = Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + fname;
FileOutputStream out = null;
if(resultCode == RESULT_OK){
if(requestCode == REQUEST_ACTION_PICK){
try {
InputStream iStream = getContentResolver().openInputStream(data.getData());
ByteArrayOutputStream os = new ByteArrayOutputStream();
Bitmap bm = BitmapFactory.decodeStream(iStream);
bm.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
byte[] w = os.toByteArray();
os.close();
iStream.close();
out = new FileOutputStream(fileFullPath);
out.write(w, 0, w.length);
out.flush();
Uri uri = Uri.fromFile(new File(fileFullPath));
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setClassName(PACKAGE_NAME, CLASS_NAME);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, uri);
//Bitmapで普通に利用ができます。
//((ImageView)findViewById(R.id.imageView1)).setImageBitmap(bm);
startActivity(intent);
}catch (IOException e) {
Log.d("test_error",e.getMessage());
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment