我有2项活动。 1,我使用相机捕获图像并将其保存在内存中,并使用路径在imageview中显示图像。 所以我想在第二个活动的另一个图像视图中再次显示相同的图像。 我怎么能这样做?
这是第一个活动
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 , ){
//}
}
这是第二项活动
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);
// }
}
答案 0 :(得分:3)
嘿Shiva这里有很多选项,第一个是从url创建文件然后是位图并将其传递给图像视图
和我建议使用图像加载器库的第二个解决方案,这对于本地或远程图像加载有帮助,我认为最好的库是Glide image loader
获取图像路径后,在第二个活动中可以添加这两个选项中的一个
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);