Reputation: 331
i have 2 activity. 1 in which i capture image using camera and save it in memory and displays the image in imageview using path . so i want to display the same image again in another image view of 2nd activity. how can i do that?
here is the 1st 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 , ){
//}
}
here is the 2nd 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);
// }
}
Upvotes: 1
Views: 1865
Reputation: 3876
Hey Shiva here you have many options the first one is to create a file from the url then a bitmap and pass it to the image view
and the second solution which i recommend it to use image loader library which is helpful in case of local or remote images loading and the best library in my opinion is Glide image loader
in your second activity after getting the image path you can add one of those two options
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);
}
or
File imgFile = new File(image);
Glide.with(getApplicationContext()).load(imgFile).into(imageview);
and here is the code to rotate the bitmap
Matrix matrix = new Matrix();
matrix.postRotate(180);
Bitmap b=Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
Upvotes: 3