简体   繁体   English

如何使用图像路径从一个活动传递到另一个活动?

[英]how do i pass image using image path from one activity to another?

i have 2 activity. 我有2项活动。 1 in which i capture image using camera and save it in memory and displays the image in imageview using path . 1,我使用相机捕获图像并将其保存在内存中,并使用路径在imageview中显示图像。 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);

   // }


}

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 嘿Shiva这里有很多选项,第一个是从url创建文件然后是位图并将其传递给图像视图

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 我建议使用图像加载库的第二个解决方案,这对于本地或远程图像加载有帮助,我认为最好的库是Glide图像加载器

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);

我如何通过 ArrayList<object> 从一个活动到另一个活动<div id="text_translate"><p>我想将 Object 的 ArrayList 从一个活动传递到另一个活动。</p><p> 这是示例代码:</p><pre> { ArrayList&lt;object&gt; list=new ArrayList&lt;&gt;(); Object value[]=new Object[m.size()]; int n=0; value[]=new Object[n]; value[n]=data.getName("name"); value[n]=data.getLocation("location"); list.add(value[n]); n++; //Since there are "n" number of object } //here the value a which is ArrayList Object // which I want to pass it from here to another activity.</pre><p> 那么现在我将如何将 object 从这里传递给另一个活动。 <strong>但是</strong>,我可以使用<strong>Gson</strong>获得值。 通过将值转换为<strong>json</strong>并将其作为字符串传递给<strong>SharedPreference</strong>并从另一个活动中检索,然后使用<strong>类型</strong>从<strong>Json</strong>转换回来以支持其原始形式。</p><p> 我想知道是否可以使用<strong>Parceable</strong>传递值。 因为我在使用它时得到了 null 值。</p><p> 这是我试图使我的 Object 变量 Parceable 的代码:</p><pre> public class ProductList implements Parcelable { private String NAME; private String LOCATION; public ProductList() { } protected ProductList(Parcel in) { LOCATION= in.readString(); NAME= in.readString(); } public static final Creator&lt;ProductList&gt; CREATOR = new Creator&lt;ProductList&gt;() { @Override public ProductList createFromParcel(Parcel in) { return new ProductList(in); } @Override public ProductList[] newArray(int size) { return new ProductList[size]; } }; public String getNAME() { return NAME; } public void setNAME(String NAME) { this.NAME= NAME; } public String getLOCATION() { return LOCATION; } public void setITEM_IMAGE(String LOCATION) { this.LOCATION= LOCATION; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(NAME); dest.writeString(LOCATION); } }</pre></div></object> - How do I pass ArrayList<Object> from one Activity to another Activity

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将下载的图像从一个活动传递到下一个活动? - How do I pass a downloaded image from one activity to the next? 我如何通过 ArrayList<object> 从一个活动到另一个活动<div id="text_translate"><p>我想将 Object 的 ArrayList 从一个活动传递到另一个活动。</p><p> 这是示例代码:</p><pre> { ArrayList&lt;object&gt; list=new ArrayList&lt;&gt;(); Object value[]=new Object[m.size()]; int n=0; value[]=new Object[n]; value[n]=data.getName("name"); value[n]=data.getLocation("location"); list.add(value[n]); n++; //Since there are "n" number of object } //here the value a which is ArrayList Object // which I want to pass it from here to another activity.</pre><p> 那么现在我将如何将 object 从这里传递给另一个活动。 <strong>但是</strong>,我可以使用<strong>Gson</strong>获得值。 通过将值转换为<strong>json</strong>并将其作为字符串传递给<strong>SharedPreference</strong>并从另一个活动中检索,然后使用<strong>类型</strong>从<strong>Json</strong>转换回来以支持其原始形式。</p><p> 我想知道是否可以使用<strong>Parceable</strong>传递值。 因为我在使用它时得到了 null 值。</p><p> 这是我试图使我的 Object 变量 Parceable 的代码:</p><pre> public class ProductList implements Parcelable { private String NAME; private String LOCATION; public ProductList() { } protected ProductList(Parcel in) { LOCATION= in.readString(); NAME= in.readString(); } public static final Creator&lt;ProductList&gt; CREATOR = new Creator&lt;ProductList&gt;() { @Override public ProductList createFromParcel(Parcel in) { return new ProductList(in); } @Override public ProductList[] newArray(int size) { return new ProductList[size]; } }; public String getNAME() { return NAME; } public void setNAME(String NAME) { this.NAME= NAME; } public String getLOCATION() { return LOCATION; } public void setITEM_IMAGE(String LOCATION) { this.LOCATION= LOCATION; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(NAME); dest.writeString(LOCATION); } }</pre></div></object> - How do I pass ArrayList<Object> from one Activity to another Activity 我如何将数据从一个活动传递到另一活动 - how i pass data from one activity to another activity 如何在一个活动中将图像从ImageView发送到另一个活动中的ImageButton。 Android Studio - How can I send an image from an ImageView in one activity to an ImageButton in another activity. Android Studio 如何将捕获的图像从自定义相机传递到另一个活动? - How to pass a captured image from custom camera to another activity? 如何将从相机或画廊获得的图像传递到另一个活动 - How to pass image gotten from camera or gallery to another activity 如何在Kitkat Android中将图像从一个活动发送到另一个活动? - How to send image from one activity to another in kitkat android? 如何在Android中将图像从一个活动发送到另一个活动 - How to send image from one activity to another in android 如何将 Uri 图像从一个活动转移到另一个活动? - How to transfer a Uri image from one activity to another? 将图像从一项活动发送到另一项活动 - send image from one activity to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM