将图像从第一个活动传递到第二个活动

时间:2015-05-23 22:25:58

标签: java android parse-platform

快速提问。我有问题将图像从第一个活动传递到 第二项活动。

在第一项活动中,我有这段代码:

   gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            ParseObject todo = urgentTodosAdapter.getItem(position);

           String about = todo.getString("comment");
            String image = todo.getString("image");


            Intent intent = new Intent("com.name.package.DetailsActivity");
            intent.putExtra("about",about);
            intent.putExtra("image",image);
            startActivity(intent);

        }

    });

此代码完全传递了第一个视图中的文本,但图像无法正常工作,我不知道添加了什么。这是第二项活动;

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.details_activity);

    Intent i = getIntent();


    String title = i.getStringExtra("about");
    String thumb = i.getStringExtra("image");



    TextView titleTextView = (TextView) findViewById(R.id.title);
    titleTextView.setText(title);

   ParseImageView imageView = (ParseImageView) findViewById(R.id.image);

   ParseFile imageFile = getParseFile("thumb"); (getParseFile is showing up red, meaning error)
    if (imageFile != null) {
        imageView.setParseFile(imageFile);
        imageView.loadInBackground();
    }

    // imageView.setImageBitmap(bitmap);
}

有什么问题?

1 个答案:

答案 0 :(得分:1)

好吧,让我们试试这样的事情

第一项活动:

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


           ParseObject todo = urgentTodosAdapter.getItem(position);

           //get the ParseFile here and then get the url in a string
           ParseFile image = todo.getParseFile("image"); //getParseFile() is a method of ParseObject
           String imageUrl = image.getUrl();

           String about = todo.getString("comment");


            Intent intent = new Intent("com.package.name.DetailsActivity");
            intent.putExtra("about",about);
            intent.putExtra("image",imageUrl); //pass along imageUrl
            startActivity(intent);

        }

    });

第二项活动:

    private ImageView mImageView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details_activity);

        Intent i = getIntent();

        String title = i.getStringExtra("about");
        String url = i.getStringExtra("image"); //get the url String

        TextView titleTextView = (TextView) findViewById(R.id.title);
        titleTextView.setText(title);

        //I switched from ParseImageView to regular ImageView here
        mImageView = (ImageView) findViewById(R.id.image);

        new DownloadImageTask(url).execute();
    }

public class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> {

    private String imageUrl;

    public DownloadImageTask(String url) {
        this.imageUrl = url;
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            // Log exception
            return null;
        }
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        mImageView.setImageBitmap(result);
    }    
}