6

独自のImageViewerを作成しましたが、AndroidネイティブのImageViewerのような機能としてSetを使用したいと考えています。Facebookにあるので今は可能です。わかりやすくするためにスクリーンショットを添付しました。ここに画像の説明を入力してください

PS何がうまくいかないのかについてもっと詳しく説明したいと思います。メニューで「連絡先アイコン」を選択すると、連絡先のリストが表示されます。連絡先を選択すると、適用力が閉じます。「ホーム/ロック画面の壁紙」を選択すると、携帯電話のギャラリーが開きます。これが私のコードスニペットです:

                Bitmap icon = mBitmap;
                Intent setAs = new Intent(Intent.ACTION_ATTACH_DATA);
                setAs.setType("image/jpg");
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                File f = new File(Environment.getExternalStorageDirectory() + File.separator + "/my_tmp_file.jpg");
                try {
                    f.createNewFile();
                    FileOutputStream fo = new FileOutputStream(f);
                    fo.write(bytes.toByteArray());
                } catch (IOException e) {                       
                    e.printStackTrace();
                }
                setAs.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/my_tmp_file.jpg"));
                startActivity(Intent.createChooser(setAs, "Set Image As"));

また、マニフェストに結果として許可を追加し、電話のSDカードに画像を書き込むことができます。

LogCat出力

4

5 に答える 5

4

Google ギャラリー アプリのソース コードから:

// Called when "Set as" is clicked.
private static boolean onSetAsClicked(MenuInvoker onInvoke,
                                      final Activity activity) {
    onInvoke.run(new MenuCallback() {
        public void run(Uri u, IImage image) {
            if (u == null || image == null) {
                return;
            }

            Intent intent = Util.createSetAsIntent(image);
            activity.startActivity(Intent.createChooser(intent,
                    activity.getText(R.string.setImage)));
        }
    });
    return true;
}

Utils.java から

// Returns an intent which is used for "set as" menu items.
public static Intent createSetAsIntent(IImage image) {
    Uri u = image.fullSizeImageUri();
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    intent.setDataAndType(u, image.getMimeType());
    intent.putExtra("mimeType", image.getMimeType());
    return intent;
}
于 2011-09-05T08:56:51.333 に答える
3

連絡先アプリのコードを見てみましょう。AttachImage画像を添付するために起動するアクティビティがあります。アイコンの写真は 96x96 ピクセルのサイズである必要があります。はaction...CROP、渡された画像に対して顔検出とトリミングを行います。

リンク:AttachImage.java

画像を 96x96 にスケーリングおよびトリミングし、その URI をアクティビティinsertPhotoで使用されるメソッドに渡す必要がありAttachImageます。壁紙の変更については、この質問

の回答 を参照できます。

アップデート

クロッピング アクティビティを起動するコード:

Intent intent = new Intent("com.android.camera.action.CROP", myIntent.getData());
if (myIntent.getStringExtra("mimeType") != null) {
   intent.setDataAndType(myIntent.getData(), myIntent.getStringExtra("mimeType"));
}
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_CROP_PHOTO);
于 2011-09-06T12:13:33.500 に答える
1

あなたは単にWallpaperManagerを使用して壁紙を設定することができます。

WallpaperManager.getInstance(this).setBitmap(mBitmap);
于 2011-09-10T19:41:29.600 に答える
1

このコードを使用してください

File externalFile=new File("filePath");
Uri sendUri = Uri.fromFile(externalFile);
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
            intent.setDataAndType(sendUri, "image/jpg");
            intent.putExtra("mimeType", "image/jpg");
            startActivityForResult(Intent.createChooser(intent, "Set As"), 200);
于 2015-07-01T10:50:32.890 に答える