ただ、IS03に標準搭載されてるcom.cooliris.mediaギャラリーのPiscaオンラインアルバムから選択された画像はトリミング出来ませんでした。
なんか毎回オンラインから取得しているみたいでローカルにキャッシュファイル持ってないのかな?
Uri返してくれないので非対応。
動かない機種とかあったらご連絡頂けるととってもアリガタヤー
-ImageCropActivity.java
/*
* ImageCropActivity
*
* Android標準のギャラリーアプリを利用して
* 画像の選択→選択画像のトリミング
* を実行。
*/
public class ImageCropActivity extends Activity {
public final static int ID_IMAGE_GALLERY = 1;
public final static int ID_IMAGE_CROP = 2;
private int width = 0;
private int height = 0;
private String bgimage = "bg.png";
ImageView layout = null;
Button button = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (ImageView) findViewById(R.id.MainRootBgr);
button = (Button) findViewById(R.id.MainCropBtn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 画像選択アプリの呼び出し
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
intent = Intent.createChooser(intent, "Select Gallery App");
startActivityForResult(intent, ID_IMAGE_GALLERY);
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
width = layout.getWidth();
height = layout.getHeight();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case ID_IMAGE_GALLERY:
Uri uri = data.getData();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
intent.setData(uri);
intent.putExtra("outputX", width);
intent.putExtra("outputY", height);
intent.putExtra("aspectX", width);
intent.putExtra("aspectY", height);
intent.putExtra("scale", true);
intent.putExtra("setWallpaper", false);
intent.putExtra("noFaceDetection", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, "");
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.name());
intent = Intent.createChooser(intent, "Select Crop App");
startActivityForResult(intent, ID_IMAGE_CROP);
break;
case ID_IMAGE_CROP:
try {
// 一時ファイル場所のUri取得処理
Uri mUri = data.getData();
if (mUri == null) {
String action = data.getAction();
if (action != null && action.indexOf("content://") > -1) {
mUri = Uri.parse(action);
}
}
// uriがしっかりと取れているようなら/files/の領域へコピーして一時保存用削除
if (mUri != null) {
ContentResolver cr = getContentResolver();
String[] columns = { MediaColumns.DATA };
Cursor c = cr.query(mUri, columns, null, null, null);
if (c != null && c.moveToFirst()) {
// 一時ファイル
File ifilepath = new File(c.getString(0));
// ローカル保存用ファイル
File ofilepath = new File(getFileStreamPath(bgimage).getPath());
FileChannel ifile = new FileInputStream(ifilepath).getChannel();
FileChannel ofile = new FileOutputStream(ofilepath).getChannel();
// ファイルコピー
ifile.transferTo(0, ifile.size(), ofile);
// クローズ処理
ifile.close();
ofile.close();
// 一時ファイルの削除
getContentResolver().delete(mUri, null, null);
if (layout != null) {
layout.setBackgroundDrawable(rntBGImage());
}
Toast.makeText(this, "Complete", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Miss", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
break;
}
}
}
private Drawable rntBGImage() {
Drawable drawable = null;
try {
// 初期化
byte[] buf = new byte[1024];
// 背景ファイル読み込み
InputStream in;
in = openFileInput(bgimage);
BufferedInputStream ins = new BufferedInputStream(in);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// ループさせて読み込み
while (true) {
int ss = ins.read(buf);
if (ss <= 0)
break;
bos.write(buf, 0, ss);
}
// Drawableに配列をデコード
drawable = new BitmapDrawable(BitmapFactory.decodeByteArray(bos.toByteArray(), 0,
bos.size()));
in.close();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return drawable;
}
}
-main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainRootLnr"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/MainRootBgr"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/MainCropBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CropStart" />
</RelativeLayout>
0 件のコメント:
コメントを投稿