Android 如何访问内部存储的图像

Android 如何访问内部存储的图像,android,android-intent,android-image,Android,Android Intent,Android Image,我有一个应用程序,它可以拍摄照片并将照片存储在我创建的文件夹中。拍完一张照片后,我希望能够访问它,以便通过电子邮件发送。如何访问我刚刚拍摄的图像?以下是我在拍摄后在内部保存图像的代码: protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode

我有一个应用程序,它可以拍摄照片并将照片存储在我创建的文件夹中。拍完一张照片后,我希望能够访问它,以便通过电子邮件发送。如何访问我刚刚拍摄的图像?以下是我在拍摄后在内部保存图像的代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");
        iv.setImageBitmap(bmp);

        File storagePath = new File(
                Environment.getExternalStorageDirectory() + "/DavePics/");
        storagePath.mkdirs();

        File myImage = new File(storagePath, Long.toString(System
                .currentTimeMillis()) + ".jpg");


        Bitmap b = Bitmap.createScaledBitmap(bmp, 320, 480, false);

        try {
            FileOutputStream out = new FileOutputStream(myImage);
            b.compress(Bitmap.CompressFormat.JPEG, 80, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

}
当我检查我创建的文件夹时,图片就在那里。我现在想做的是访问该图片,以便我可以通过电子邮件从下面的代码发送它:

@Override
public void onClick(View v) {

    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bSendPic:

        String emailaddress[] = { "info@sklep.com", "", };

        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_EMAIL, emailaddress);

        //emailIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, pic);

        emailIntent.setType("image/jpeg");
        startActivity(Intent.createChooser(emailIntent, "Send Mail"));

        break;
    case R.id.ibTakePic:

        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(i, cameraData);
        break;
    }

}
如何访问此图片,以便将其添加到电子邮件中?我这样做对吗?谢谢

编辑:这是我的全部代码

public class Camera extends Activity implements View.OnClickListener {

ImageButton ib;
Button b;
ImageView iv;
Intent i;
final static int cameraData = 0;
Bitmap bmp;
File myImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photo);
    initialize();
    InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
    bmp = BitmapFactory.decodeStream(is);

}

private void initialize() {
    ib = (ImageButton) findViewById(R.id.ibTakePic);
    b = (Button) findViewById(R.id.bSendPic);
    iv = (ImageView) findViewById(R.id.ivReturnedPic);
    b.setOnClickListener(this);
    ib.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bSendPic:

        if (myImage.exists()) {

            String emailaddress[] = { "info@sklep.com", "", };

            Intent emailIntent = new Intent(Intent.ACTION_SEND);
            emailIntent.putExtra(Intent.EXTRA_EMAIL, emailaddress);
            emailIntent.setType("image/jpeg");
            emailIntent
                    .putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myImage));
            startActivity(Intent.createChooser(emailIntent, "Send Mail"));

        }

        break;
    case R.id.ibTakePic:

        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(i, cameraData);
        break;
    }

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");
        iv.setImageBitmap(bmp);

        File storagePath = new File(
                Environment.getExternalStorageDirectory() + "/DavePics/");
        storagePath.mkdirs();

        myImage = new File(storagePath, Long.toString(System
                .currentTimeMillis()) + ".jpg");

        Bitmap b = Bitmap.createScaledBitmap(bmp, 320, 480, false);

        try {
            FileOutputStream out = new FileOutputStream(myImage);
            b.compress(Bitmap.CompressFormat.JPEG, 80, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

}

您忘记的代码行是

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myImage));
在代码中,在活动中全局声明
File myImage

现在在电子邮件发送代码

检查文件是否存在

if(myImage.exist())
{
 String emailaddress[] = { "info@sklep.com", "", };
 Intent emailIntent = new Intent(Intent.ACTION_SEND);
 emailIntent.putExtra(Intent.EXTRA_EMAIL, emailaddress);
 emailIntent.setType("image/jpeg");
 emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myImage));
 startActivity(Intent.createChooser(emailIntent, "Send Mail"));
}

谢谢我这样做,当我去发送电子邮件时,你可以看到附件已经添加。然而,当我发送电子邮件时,它只是说发送电子邮件,然后它就再也不发送了。我已附上我的完整代码。有什么想法吗?我知道这是可行的。万分感谢!我的gmail帐户一直冻结,但一旦我清除缓存并重新启动它,它就工作得很好!欢迎你,伙计。。!快乐的编码。。!