gpt4 book ai didi

android - 想要将图像从一个 Activity 转移到另一个 Activity

转载 作者:行者123 更新时间:2023-11-29 15:38:13 27 4
gpt4 key购买 nike

在这里,我有两个 Activity。在第一个 Activity 中,我想从图库中选择一张图片并将该图片传输到第二个 Activity 并设置该 Activity 的 ImageView 。

我有意将图像路径转移到另一个 Activity ,但它不适用于所有图像。我只能从图库中获取一些图片,对于一些图片,它会关闭我的应用程序或不显示任何内容并返回到第一个 Activity 。

我如何改进我的代码以适用于所有图像。我检查了很多次,我认为问题出在传输文件路径并将其放入另一个 Activity 中。

我的第一个 Activity :

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button choose;
private Bitmap bitmap;

private int PICK_IMAGE_REQUEST = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

choose = (Button) findViewById(R.id.choose);
choose.setOnClickListener(this);
}


@Override
public void onClick(View view) {
if (view == choose) {
showFileChooser();
}
}

private void showFileChooser() {


Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();


try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("picture", bitmap);
startActivity(i);
}
catch (IOException e) {
e.printStackTrace();


}
}

}
}

我的第二个 Activity

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

ImageView image;
Button upload;

Bitmap bitmap;

SharedPreferences sp;
String rollno;

private String UPLOAD_URL ="http://aptronnoida.com/applock/image_insert.php";


private String KEY_Rollno = "rollno";
private String KEY_IMAGE = "image";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
image=(ImageView)findViewById(R.id.image);

bitmap= getIntent().getParcelableExtra("picture") ;
image.setImageBitmap(bitmap);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);


upload = (Button) findViewById(R.id.upload);
upload.setOnClickListener(this);
sp=getSharedPreferences("rajput",MODE_PRIVATE);
rollno=sp.getString("rollno",null);
}



@Override
public void onClick(View view) {
if(view == upload){
uploadImage();
}
}

public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}

private void uploadImage() {

final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
//Disimissing the progress dialog
loading.dismiss();
//Showing toast message of the response
Toast.makeText(SecondActivity.this, s , Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Dismissing the progress dialog
loading.dismiss();

//Showing toast
Toast.makeText(SecondActivity.this, "Error", Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
String image = getStringImage(bitmap);

//Creating parameters
Map<String,String> params = new Hashtable<String, String>();

//Adding parameters
/*params.put(KEY_Rollno,rollno);*/
params.put(KEY_IMAGE, image);


//returning parameters
return params;
}
};

//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);

//Adding request to the queue
requestQueue.add(stringRequest);
}


@Override
public boolean onSupportNavigateUp(){
onBackPressed();
return true;
}
public void onBackPressed(){

super.finish();
}


}

最佳答案

您可以将图像路径而不是整个图像传递给second activity,并在初始化时将其加载到第二个activity。您可以使用 glidepicasso如果库的使用对您来说不是问题,则用于加载图像的库。

步骤:

  1. > get the path of image
  2. 保存在字符串变量中
  3. 创建一个Bundle对象,将路径字符串放入bundle中。
  4. 使用该包开始第二个 Activity 。
  5. 在第二个 Activity 中从包中获取路径值。最后,
  6. 使用 glide 或 picasso 加载图像。

关于android - 想要将图像从一个 Activity 转移到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45904248/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com