将图片从活动传递到另一个活动

时间:2015-04-16 16:04:34

标签: android bitmap transfer

我有三个Activity个对象。 我希望通过传入FirstActivity

将照片从SecondActivity转移到AlarmREceiver

这是我的FirstActivity

代码
package com.testcamera.hassanechafai.testcamera;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.util.Calendar;
import java.util.Date;


public class FirstActivity extends ActionBarActivity {

    private String selectedImagePath = "";
    final private int PICK_IMAGE = 1;
    final private int CAPTURE_IMAGE = 2;
    ImageView imgView;
    private String imgPath;
    Intent myIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        imgView = (ImageView) findViewById(R.id.ImageView);
        Button butCamera = (Button) findViewById(R.id.Button1);
        butCamera.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final Intent intent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        setImageUri());
                startActivityForResult(intent, CAPTURE_IMAGE);
            }
        });

        Button butGallery = (Button) findViewById(R.id.Button2);
        butGallery.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, ""),
                        PICK_IMAGE);
            }
        });

        final EditText save = (EditText) findViewById(R.id.EditText1);
        Button myBtn = (Button) findViewById(R.id.Save);


        myBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int time = Integer.parseInt(save.getText().toString());
                if (time > 0) {

                     myIntent = new Intent(getBaseContext(), AlarmReceiver.class);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, myIntent, 0);
                    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(System.currentTimeMillis());
                    calendar.add(Calendar.SECOND, time);
                    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
                    Toast.makeText(getApplicationContext(), "Starting Activity in: " + time + " seconds", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });
    }


            public Uri setImageUri() {
                // Store image in dcim
                File file = new File(Environment.getExternalStorageDirectory()
                        + "/DCIM/", "image" + new Date().getTime() + ".png");
                Uri imgUri = Uri.fromFile(file);
                this.imgPath = file.getAbsolutePath();
                return imgUri;
            }

            public String getImagePath() {
                return imgPath;
            }

            @Override
            protected void onActivityResult(int requestCode, int resultCode,
                                            Intent data) {
                if (resultCode != Activity.RESULT_CANCELED) {
                    if (requestCode == PICK_IMAGE) {
                        selectedImagePath = getAbsolutePath(data.getData());
                        imgView.setImageBitmap(decodeFile(selectedImagePath));

                    } else if (requestCode == CAPTURE_IMAGE) {
                        selectedImagePath = getImagePath();
                        imgView.setImageBitmap(decodeFile(selectedImagePath));
                        Intent intent = new Intent(this, CallActivity.class);
                        intent.putExtra("BitmapImage", selectedImagePath);
                    } else {
                        super.onActivityResult(requestCode, resultCode,
                                data);
                    }
                }

            }



            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();
                if (id == R.id.action_settings) {
                    return true;
                }
                return super.onOptionsItemSelected(item);
            }

            public Bitmap decodeFile(String path) {
                try {
                    // Decode image size
                    BitmapFactory.Options o = new BitmapFactory.Options();
                    o.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(path, o);

                    // The new size we want to scale to
                    final int REQUIRED_SIZE = 70;

                    // Find the correct scale value. It should be the power of
                    // 2.
                    int scale = 1;
                    while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                            && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                        scale *= 2;

                    // Decode with inSampleSize
                    BitmapFactory.Options o2 = new BitmapFactory.Options();
                    o2.inSampleSize = scale;
                    return BitmapFactory.decodeFile(path, o2);
                } catch (Throwable e) {
                    e.printStackTrace();
                }
                return null;

            }

            public String getAbsolutePath(Uri uri) {
                String[] projection = { MediaColumns.DATA };
                @SuppressWarnings("deprecation")
                Cursor cursor = managedQuery(uri, projection, null, null, null);
                if (cursor != null) {
                    int column_index = cursor
                            .getColumnIndexOrThrow(MediaColumns.DATA);
                    cursor.moveToFirst();
                    return cursor.getString(column_index);
                } else
                    return null;
            }

        }

这个AlarmReceiver代码

package com.testcamera.hassanechafai.testcamera;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Alarm time reached", Toast.LENGTH_SHORT).show();
        Intent i = new Intent();
        i.setClassName("com.testcamera.hassanechafai.testcamera", "com.testcamera.hassanechafai.testcamera.CallActivity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

这个SecondAcitivy代码(我称之为CallActivity)

package com.testcamera.hassanechafai.testcamera;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;


public class CallActivity extends ActionBarActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call);
        ImageView image = (ImageView)findViewById(R.id.ImageView);



    }

我需要通过传递AlarmActivity将照片从FirstActivity转移到SecondAcitivy,有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

myBtn onClick您忘记添加myIntent.putExtra("theKeyUsed","yourConvertedStringUri");

然后在你的Receiver课程中你也错过了。

CallAcitvity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call);
    String path = getIntent().getStringExtra("theKeyUsed"); // in your case  "BitmapImage"
    ImageView image = (ImageView)findViewById(R.id.ImageView);
    // now set image using the path
}

一般情况下,将您的uri.toString()作为一个额外的字符串传递给您,并将其用于首选活动并检索..

如果找到解决方案,请随时删除该问题