あるアクティビティから別のアクティビティへ画像を転送する必要があります。第1のアクティビティでは、ユーザはスクロールビューから複数の画像の中から画像を選択し、その画像を次のアクティビティの画像ビューに表示しなければならない。ヘルプが必要です。あなたの最初の活動にあるアクティビティから別のアクティビティへ画像を転送したい
答えて
コンバートImageViewのは
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
が続いてImageViewの中にビットマップを表示する第二の活動では
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
をビットマップに。
注:これはお勧めできません。実際に画像をどこかに保存し、代わりにパスを渡して2番目のアクティビティから取得する必要があります。
これはputExtraコマンドでエラーが発生していないため、id "imageview"を持つイメージビューのイメージをid "images"を持つ次のアクティビティのイメージビューに転送する必要があります。 –
あなたのお手伝いをする私の答えが更新されました。 – Raghunandan
ありがとう@Raghunandan:私はこの質問のために多くを捜した、それらのどれも助けなかった、あなたの答えは最後に私のために働いた。ありがとう –
イメージのURIを次のアクティビティに渡すことができます。
あなたは()onActivityResultから取得URI
し、次の活動ののonCreate()で
。は再びビットマップをデコードし、バイト配列として渡すと、次のアクティビティに表示のためのビットマップを構築することができImageViewの
に設定します。例えば:あなたの最初の活動で
:
Intent i = new Intent(this, NextActivity.class);
Bitmap b; // your bitmap
ByteArrayOutputStream bs = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("byteArray", bs.toByteArray());
startActivity(i);
2番目の活動で
if(getIntent().hasExtra("byteArray")) {
ImageView previewThumbnail = new ImageView(this);
Bitmap b = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
previewThumbnail.setImageBitmap(b);
}
putextraが動作していません –
はthis記事を参照してください。
2番目のアクティビティを開始するときに最初のアクティビティにputExtraを使用します。そして、次のように第2の活性使用getExtra()で:
最初の活動で
Intent intent = new Intent(Activity1.this,Activity2.class);
intent.putExtra("bmp",selectedImage);
startActivity(intent);
セカンド活動に
Bitmap bmp = this.getIntent().getParcelableExtra("bmp");
実際にputextraは動作していません –
ByteArrayOutputStream bs = new ByteArrayOutputStream(); b.compress(Bitmap.CompressFormat.PNG、50、bs); putExtras()の前の 。 –
あなたは多くの方法でこれを行うことができます。単純なのは意図です。しかし、それはあなたのデバイスをハングアップすることができますまた、あなたはギャラクシーS3のような多くのデバイスでメモリの例外を与える。
ですから、私はあなたに非常に簡単な方法を教えてください。
あなたのような一つのクラスでstatic
変数を作成することができます。
public class ABC{
public static Bitmap PHOTO = null;
}
あなたは、ギャラリーや他の方法からビットマップを取得するときに、今、あなたはこの写真の変数にビットマップを保存する必要があります(これはのみonActivityResultで可能です、私はそうですか?)
カメラから写真を取得した場合はコードです。
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
と、
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case CAMERA_PIC_REQUEST:
Bitmap b = (Bitmap) data.getExtras().get("data");
if (b != null) {
ABC.PHOTO = b;
}
break;
}
}
及び他のアクティビティにこの写真の変数を使用します。
これは、ギャラリーから写真を選択するときと同じ方法で使用できます。
これは編集されたansです。
これはグリッドビューの単なるサンプルの例です。ここでは、1つのアクティビティから別のアクティビティにイメージを渡す方法についてのアイデアが得られます。
これはあなたのメインのActivityクラスです:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView android:id="@+id/gridView1" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:numColumns="4"></GridView>
</LinearLayout>
newActivity.class
package com.GridViewDemo;
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
public class newActiivty extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.new_layout);
String image_path = getIntent().getStringExtra("Image_Path");
ImageView imageview = (ImageView) findViewById(R.id.imageView1);
Drawable d = LoadImageFromWebOperations(image_path);
if (d != null) {
imageview.setImageDrawable(d);
} else {
imageview.setImageResource(R.drawable.icon);
}
/** if you have bitmap then
* imageview.setImageBitmap(test.PHOTO);
* */
}
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
}
new_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:src="@drawable/icon" android:id="@+id/imageView1"
android:layout_width="fill_parent" android:layout_height="320dp"></ImageView>
</LinearLayout>
package com.GridViewDemo;
import java.io.InputStream;
import java.net.URL;
import java.util.GregorianCalendar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class GridViewDemoActivity extends Activity {
/** Called when the activity is first created. */
// String[] mArr =
// {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
String[] mArr = {
"http://www.xda-developers.com/wp-content/uploads/2012/10/androidfigure.jpg?f39ce1",
"http://1.bp.blogspot.com/-Ramp-o0Cp8s/T0VafXkE4uI/AAAAAAAAAQU/i703zg5MBgI/s1600/android-wallpaper5_1024x768.jpg",
"http://www.thebiblescholar.com/android_awesome.jpg",
"http://blogs-images.forbes.com/rogerkay/files/2011/07/Android1.jpg" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
/** if you have bitmap here then you can use this way
* Bitmap bitmap = getBitmap();
* test.PHOTO = bitmap;
*
* */
Intent i = new Intent(GridViewDemoActivity.this, newActiivty.class);
i.putExtra("Image_Path", ""+mArr[arg2]);
startActivity(i);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mArr.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imgView;
if (convertView == null) {
imgView = new ImageView(mContext);
imgView.setLayoutParams(new GridView.LayoutParams(85, 85));
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgView.setPadding(8, 8, 8, 8);
} else {
imgView = (ImageView) convertView;
}
Drawable d = LoadImageFromWebOperations(mArr[position]);
if (d == null) {
imgView.setImageResource(R.drawable.icon);
} else {
imgView.setImageDrawable(d);
}
return imgView;
}
}
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
}
main.xml
マニフェストファイル
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.GridViewDemo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".GridViewDemoActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".newActiivty"></activity>
</application>
</manifest>
これは、余分なクラスです:あなたはビットマップを持っている場合は、この方法を使用します。
package com.GridViewDemo;
import android.graphics.Bitmap;
public class test {
public static Bitmap PHOTO = null;
}
私はコード内のコメントは、それをチェックして、クエリを持っている場合は、このANS以下のコメント。
これは、最初のアクティビティでユーザーが[OK]ボタンを押したときに呼び出される関数のコードです。public void ok(ビューを表示){ \t Intent intent = new Intent(this、Data.class); EditText editText =(EditText)findViewById(R.id.text); 文字列メッセージ= editText.getText()。toString(); intent.putExtra(EXTRA_MESSAGE、message); startActivity(インテント); インテントintent1 =新しいインテント(Template.this、Data.class); ImageViewイメージ=(ImageView)findViewById(R.id.imageview); intent1.putExtra( "bmp"、image); startActivity(インテント); } –
これは間違っています。あなたのすべてのコードをあなたの質問に入れてください。あなたはimageViewを渡しています。スクロールビューでイメージを表示するために使用しているイメージのビットマップまたはパスを渡す必要があります。 –
私はimageviewのIDが最初のアクティビティでイメージビューをイメージしなければなりません。コード –
base64
文字列形式でイメージを変換し、次のアクティビティに渡して、再びビットマップにデコードします。第二の活性の
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
Intent showImageIntent = new Intent(this, ShowCameraPicture.class);
showImageIntent.fillIn(data,Intent.FILL_IN_DATA);
startActivity(showImageIntent);
}
}
:最初の活性の
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
if(bundle!=null){
Bitmap image = (Bitmap)bundle.get("data");
//do whatever you need to do with the bitmap here ...
}
}
//////////firstActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.myapp.com.app44sendinginfofromoneactivitytoanother.MainActivity">
<RelativeLayout
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical">
<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter NAme"
android:inputType="textPersonName" />
<EditText
android:id="@+id/edtLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter LastName"
android:inputType="textPersonName" />
<EditText
android:id="@+id/edtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Email"
android:inputType="textPersonName" />
<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword" />
<ImageView
android:id="@+id/imgView"
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="@android:drawable/ic_menu_report_image" />
<RadioGroup
android:id="@+id/rdoGroup"
android:layout_width="match_parent"
android:layout_height="50dp">
<RadioButton
android:id="@+id/rdoMale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Male" />
<RadioButton
android:id="@+id/rdoFemale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female" />
</RadioGroup>
<Button
android:id="@+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Send" />
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
//// ////////// MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{
EditText edtName,edtLastName,edtEmail,edtPassword;
ImageView imgView;
RadioButton rdoMale,rdoFemale;
Button btnSend;
String genderType = "";
int CAMERA_PIC_REQUEST = 99;
Bitmap bitmap; // your bitmap
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtName = (EditText) findViewById(R.id.edtName);
edtLastName = (EditText) findViewById(R.id.edtLastName);
edtEmail = (EditText) findViewById(R.id.edtEmail);
edtPassword = (EditText) findViewById(R.id.edtPassword);
btnSend = (Button) findViewById(R.id.btnSend);
imgView = (ImageView) findViewById(R.id.imgView);
rdoMale = (RadioButton) findViewById(R.id.rdoMale);
rdoFemale = (RadioButton) findViewById(R.id.rdoFemale);
btnSend.setOnClickListener(MainActivity.this);
imgView.setOnClickListener(MainActivity.this);
rdoMale.setOnCheckedChangeListener(MainActivity.this);
rdoFemale.setOnCheckedChangeListener(MainActivity.this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.rdoMale:
if (isChecked){
genderType = "Male";
}else{
buttonView.setChecked(false);
}
break;
case R.id.rdoFemale:
if (isChecked){
genderType = "Female";
}else{
buttonView.setChecked(false);
}
break;
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnSend:
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("NAME",edtName.getText().toString());
intent.putExtra("LASTNAME",edtLastName.getText().toString());
intent.putExtra("EMAIL",edtEmail.getText().toString());
intent.putExtra("PASSWORD",edtPassword.getText().toString());
intent.putExtra("GENDER",genderType);
//below is the code which you are looking for
--> ByteArrayOutputStream bs = new ByteArrayOutputStream();
--> bitmap.compress(Bitmap.CompressFormat.PNG, 100, bs);
byte[] byteArray = bs.toByteArray();
intent.putExtra("PICTURE", byteArray);
startActivity(intent);
break;
case R.id.imgView:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
break;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if (requestCode == CAMERA_PIC_REQUEST) {
bitmap = (Bitmap) data.getExtras().get("data");
imgView.setImageBitmap(bitmap);
}
}
}
//////////////////// SecondActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.myapp.com.app44sendinginfofromoneactivitytoanother.ResultActivity">
<RelativeLayout
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical">
<TextView
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="FirstName:"
android:textSize="24sp" />
<TextView
android:id="@+id/txtLast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="LastName:"
android:textSize="24sp" />
<TextView
android:id="@+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Email:"
android:textSize="24sp" />
<TextView
android:id="@+id/txtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Password:"
android:textSize="24sp" />
<TextView
android:id="@+id/txtGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Gender:"
android:textSize="24sp" />
<ImageView
android:id="@+id/imgViewResult"
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="@android:drawable/ic_menu_report_image" />
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
///////// SecondActivity。Javaの
public class SecondActivity extends AppCompatActivity {
Context ctx ;
TextView txtName,txtLast,txtEmail,txtPassword,txtGender;
ImageView imgViewResult;
Bitmap bitmap;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
ctx = this;
setContentView(R.layout.activity_result);
txtName = (TextView) findViewById(R.id.txtName);
txtLast = (TextView) findViewById(R.id.txtLast);
txtEmail = (TextView) findViewById(R.id.txtEmail);
txtPassword = (TextView) findViewById(R.id.txtPassword);
txtGender = (TextView) findViewById(R.id.txtGender);
imgViewResult = (ImageView) findViewById(R.id.imgViewResult);
Bundle extras = getIntent().getExtras();
String Name = extras.getString("NAME");
String LastName = extras.getString("LASTNAME");
String Email = extras.getString("EMAIL");
String Password = extras.getString("PASSWORD");
String Gender = extras.getString("GENDER");
txtName.setText("Name: "+Name);
txtLast.setText("LastName: "+LastName);
txtEmail.setText("Email: "+Email);
txtPassword.setText("Password: "+Password);
txtGender.setText("Gender: "+Gender);
byte[] byteArray = extras.getByteArray("PICTURE");
bitmap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
imgViewResult.setImageBitmap(bitmap);
}
}
HappyCoding :)
最初の活動 の最初のあなたは ビットマップビットマップなどの上部にビットマップのインスタンスを確かにしてください。次いで
及びこれを使用する: -
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bs);
byte[] byteArray = bs.toByteArray();
intent.putExtra("PICTURE", byteArray);
startActivity(intent);
及び第二活性が最初のアクティビティに: - 別のアクティビティからビットマップを通過
byte[] byteArray = extras.getByteArray("PICTURE");
bitmap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
imgViewResult.setImageBitmap(bitmap);
- 1. あるアクティビティから別のアクティビティへ画像を渡す
- 2. 1つのアクティビティ(データベース)から別のアクティビティへの転送
- 3. あるアクティビティから別のアクティビティへのデータの送信
- 4. リサイクラビューから他のアクティビティへ画像を送信する
- 5. あるアクティビティから別のアクティビティにデータを送信する
- 6. あるアクティビティから別のアクティビティへデータを渡す
- 7. あるアクティビティから別のアクティビティへのテキストの移動
- 8. 1つのアクティビティから別のアクティビティへデータを送信する方法。 (Bluetooth)
- 9. あるアクティビティから別のアクティビティへのANY型の配列の受け渡し
- 10. 1つのアクティビティから別のアクティビティに画像を保存する方法
- 11. 1つのアクティビティからデータベースへの変数の転送
- 12. (Android)あるアクティビティから別のアクティビティへ設定を取得する
- 13. リストを渡す<Class>アクティビティから別のアクティビティへ
- 14. サービスからアクティビティへのデータの送信
- 15. Android:別のアクティビティからのアクティビティへのアクセス方法
- 16. Mapview.drawからアクティビティへメッセージを送信
- 17. あるアクティビティから別のアクティビティに値を渡す
- 18. 私のedittextから1アクティビティから別のアクティビティへテキストを持ち帰り、空であればクリアする
- 19. あるアクティビティから別のアクティビティにオブジェクトを渡す最も良い方法
- 20. 1つのアクティビティから別のアクティビティへListViewデータを取得する方法
- 21. あるアクティビティから前のアクティビティへの文字列の受け渡し
- 22. Rails:あるスキーマから別のスキーマへデータを転送する
- 23. アイコンの画像をJLabelから別の画像に転送できません
- 24. androidのタブレイアウトで1つのアクティビティから別のアクティビティへデータを渡す方法
- 25. Android:1つのアクティビティから別のアクティビティへWebViewを渡す方法
- 26. あるフォルダから別のフォルダへの転送の許可
- 27. クライアントからサーバーへの画像(* .jpeg、* .pngなど)ファイルの転送
- 28. 新しいアクティビティでAndroidディスプレイカメラ画像
- 29. サービスからアクティビティへブロードキャストを送信しますか?
- 30. あるアクティビティから別のアクティビティへ同じxmpp接続を取得する方法は?
は悪い考えです。 パスまたはURIを渡すことを検討する必要があります。 – minhaz