gpt4 book ai didi

java - 如何使用图像路径将图像从一个 Activity 传递到另一个 Activity ?

转载 作者:行者123 更新时间:2023-11-29 04:55:49 25 4
gpt4 key购买 nike

我有 2 个 Activity 。 1,其中我使用相机捕获图像并将其保存在内存中,并使用路径在 imageview 中显示图像。所以我想在第二个 Activity 的另一个 ImageView 中再次显示相同的图像。我该怎么做?

这是第一个 Activity

package com.example.shiva_000.myproject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.io.File;
import java.net.URI;

public class MainActivity extends Activity {
public static final String Tag_Price ="tag";
public static final String TAG_Radio ="Button";
private String SelectedType ="";
String path = "sdcard/camera_app/cam_image.jpg";

EditText et;

Button button;
ImageView imageView;
// Radio button
Button button2;
RadioGroup radio_g;
RadioButton rb1, rb2;



static final int cam_request = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button =(Button) findViewById(R.id.button1);
imageView =(ImageView) findViewById(R.id.image_view);
button2 = (Button) findViewById(R.id.submit);
et =(EditText) findViewById(R.id.editText);
radio_g=(RadioGroup) findViewById(R.id.rg1);
final RadioButton rb1 = (RadioButton) findViewById(R.id.newoption);
final RadioButton rb2 = (RadioButton) findViewById(R.id.usedoption);


// on click for submit
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {





String s = et.getText().toString();
Intent i = new Intent(MainActivity.this,secondScreen.class);
i.putExtra("Condition",SelectedType);
i.putExtra("Price",s);
i.putExtra("image",path);


startActivity(i);



}
});


// on click for capture image
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent camera_Intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_Intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_Intent, cam_request);

}

});


//launchSecondActivity(Double.parseDouble(et.getText().toString()),null);

}
//private void launchSecondActivity(double price ,Image image){



// }
public void Condition(View view) {

boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.newoption:
if (checked)
//SelectedType = rb1.getText().toString();

break;
case R.id.usedoption:
if (checked)
// SelectedType = rb2.getText().toString();

break;
}
}

private File getFile()
{
File folder = new File("sdcard/camera_app");

if (!folder.exists())
{
folder.mkdir();
}

File image_file = new File(folder,"cam_image.jpg");
return image_file;


// return null;

}


// public void

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// String path = "sdcard/camera_app/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
}

//private void launchSecondActivity(condion , ){

//}
}

这是第二个 Activity

package com.example.shiva_000.myproject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

/**
* Created by shiva_000 on 11/18/2015.
*/
public class secondScreen extends Activity {
ImageView imageView;
TextView textView1;
TextView textView2;
Button button;


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

imageView =(ImageView) findViewById(R.id.image_view);
textView1 =(TextView) findViewById(R.id.textView4);
textView2 =(TextView) findViewById(R.id.textView5);
button = (Button)findViewById(R.id.button2);
Intent ii = getIntent();
Bundle b = ii.getExtras();

if (b!=null)
{
String passed_condition = (String) b.get("Condition");
String passed_price = (String) b.get("Price");
//String passed_image = getIntent().getStringExtra("imagePath");
textView1.setText("Condition : " + passed_condition);
textView2.setText("The price is : " + passed_price + "$");
//Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
String image = ii.getStringExtra("image");
//imageView.setImageDrawable();
}

//button.setOnClickListener();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});

//String path = getIntent().getStringExtra("")






}




// private void initializesecondactivity ()
//{
// double price = getIntent().getExtras().getDouble(MainActivity.Tag_Price);

// }


}

最佳答案

嘿 Shiva,这里有很多选项,第一个是从 url 创建文件,然后是位图并将其传递给 ImageView

我推荐它使用图像加载器库的第二个解决方案,它在本地或远程图像加载的情况下很有帮助,我认为最好的库是 Glide image loader

在获取图像路径后的第二个 Activity 中,您可以添加这两个选项之一

String image = ii.getStringExtra("image");
File imgFile = new File(image);

if(imgFile.exists()){

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

myImage.setImageBitmap(myBitmap);

}

 File imgFile = new  File(image);
Glide.with(getApplicationContext()).load(imgFile).into(imageview);

这是旋转位图的代码

Matrix matrix = new Matrix();
matrix.postRotate(180);
Bitmap b=Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);

关于java - 如何使用图像路径将图像从一个 Activity 传递到另一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33794159/

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