7

私は1つのクラスにimageViewを持っています.imageViewをクリックすると、カメラから画像を取得するか、デバイスの画像ギャラリーを開くための2つのオプションがあるダイアログボックスが表示されます。ImageView に表示できるように、あるクラスから別のクラスに画像を送信したいと考えています。私は何時間も検索していますが、あるクラスから別のクラスにテキストデータを送信することしかできませんでした。あるクラスから別のクラスに画像を送信することについて誰か教えてもらえますか?

これは、画像を取得する送信者クラスのコードです。

   takeImg.setOnTouchListener(新しいOnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // TODO 自動生成メソッド スタブ
                if(event.getAction() == event.ACTION_UP)
                {
                    i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(i,cameraData);
                }
                true を返します。
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, インテント データ) {
        // TODO 自動生成メソッド スタブ
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK)
        {
            Bundle extras=data.getExtras();
            bmp=(ビットマップ)extras.get("データ");
        }
    }

助けてくれてありがとう

4

8 に答える 8

3

putExtra() と getExtra() には 1MB 程度のサイズ制限があることを覚えています。そのため、画像がこの制限を超える場合があります。写真にパスを渡すだけではどうですか?

于 2012-07-22T08:47:42.453 に答える
3

私の好みの方法 (そして最も簡単な方法だと思います) は、アプリ内で独自の Application インスタンスを使用して、複数のアクティビティに共通する変数を格納することです。

クラスを作成し、それMainApplicationを拡張android.app.Application してマニフェストで宣言します。

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:name=".MainApplication">

次に、次のようにアクティビティでアプリケーション オブジェクトのインスタンスを取得します。

MainApplication application = ((MainApplication)getApplication());

このアプリケーション オブジェクト内に、アプリ レベルのデータを保存して、通常どおり使用できます。

application.setImage(...);

application.getImage();
于 2012-07-22T09:38:10.073 に答える
3

あるアクティビティから別のアクティビティに画像のパスを送信するために必要な答えが得られました。filePath は画像のパスです。

Intent open_displayPage=new Intent(MainActivity.this,display_page.class);
open_displayPage.putExtra("imagePath", filePath);

そして、別のアクティビティでパスを取得します

final String path = getIntent().getStringExtra("imagePath");
org_bmp = BitmapFactory.decodeFile(path);
于 2013-02-17T05:25:55.783 に答える
2

1つ取りGlobal.class、宣言しpublic static Bitmap bmpます。

takeImg.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if(event.getAction() == event.ACTION_UP)
                {
                    i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(i,cameraData);
                }
                return true;
            }
        });
    }
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK)
        {
            Bundle extras=data.getExtras();
            Global.bmp=(Bitmap)extras.get("data");
        }
    }

そして、あなたが使いたいときBitmap bitmap = Global.bmp;

于 2012-07-22T09:13:25.027 に答える
2

最善の方法をお教えします。

1st) 画像の URI を取得して送信する

Uri imageUri = data.getData();
Intent newIntent = new Intent(Class.this, Class.class);
newIntent.putExtra(IMAGE_URI_KEY, imageUri);
startActivity(newIntent);

2) 画像の受け取り方と見せ方

receivedImageUri = getIntent().getParcelableExtra(IMAGE_URI_KEY);
imageView.setImageURI(receivedImageUri);
于 2014-03-18T01:38:16.710 に答える
2

トランザクション バインダーの 1 MB の制限を超えないように、ビットマップを少し再スケーリングする必要がありました。400 を画面に適応させるか、動的にすることができます。これは単なる例です。それはうまく機能し、品質はいいです。また、画像を保存してから読み込むよりもはるかに高速ですが、サイズの制限があります。

public void loadNextActivity(){
Intent confirmBMP = new Intent(this,ConfirmBMPActivity.class);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bmp = returnScaledBMP();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);

confirmBMP.putExtra("Bitmap",bmp);
startActivity(confirmBMP);
finish();

}
public Bitmap returnScaledBMP(){
Bitmap bmp=null;
bmp = tempBitmap;
bmp = createScaledBitmapKeepingAspectRatio(bmp,400);
return bmp;
}

次のコードを使用して、nextActivity で bmp を回復した後:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirmBMP);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");

}

私の答えが何らかの形で役に立てば幸いです。ご挨拶

于 2014-04-30T06:55:11.640 に答える
1

シングルトン オブジェクトを使用して画像を保存できます。

public class SingletonModel {
    private Bitmap Image;
    private SingletonModel; 
    public static SingletonModel getInstance() {
        if (instance == null) {
            instance = new SingletonModel();
        }
        return instance;
    }

    public Bitmap getImage() {
       return this.Image
    }

    public Bitmap setImage(Bitmap ImageIn) {
        this.Image = ImageIn;
    }
}

そして、最初のアクティビティに次を入れます:

SingletonModel.getInstance().setImage(image);

そして、2 番目のアクティビティで:

Bitmap image = SingletonModel.getInstance().getImage();

別の方法として、 を拡張するオブジェクトを作成することもできますApplication。そのため、このオブジェクトはすべてのクラスで表示されます (考え方はシングルトン オブジェクトと同じです)。

于 2012-07-22T09:27:40.107 に答える