如何访问存储在内部的图像 [英] How to access an image that is stored internally

查看:167
本文介绍了如何访问存储在内部的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,拍照并存储在内部的图片,我已经创建了一个文件夹中。在拍摄照片后,我希望能够访问它,这样我可以通过电子邮件发送。我如何才能访问我刚才拍摄的图像下面是我的code,节省了图像拍摄完毕后,在内部图像:

 保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,结果code,数据);
    如果(结果code == RESULT_OK){
        捆绑额外= data.getExtras();
        BMP =(位图)extras.get(数据);
        iv.setImageBitmap(BMP);        文件storagePath =新的文件(
                Environment.getExternalStorageDirectory()+/ DavePics /);
        storagePath.mkdirs();        文件MYIMAGE =新的文件(storagePath,Long.toString(系统
                .currentTimeMillis())+.JPG);
        位图B = Bitmap.createScaledBitmap(BMP,320,480,FALSE);        尝试{
            FileOutputStream中出=新的FileOutputStream(MYIMAGE);
            b.com preSS(Bitmap.Com pressFormat.JPEG,80出);
            了out.flush();
            out.close();
        }赶上(例外五){
            e.printStackTrace();
        }    }
}}

当我检查我已创建的文件夹的图片是存在的。我想现在要做的就是访问该图片,这样我可以在一封电子邮件从下我的code发送:

  @覆盖
公共无效的onClick(视图v){    // TODO自动生成方法存根
    开关(v.getId()){
    案例R.id.bSendPic:        串EMAILADDRESS [] = {info@sklep.com,,};        意图emailIntent =新意图(Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_EMAIL,EMAILADDRESS);        //emailIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,PIC);        emailIntent.setType(图像/ JPEG);
        startActivity(Intent.createChooser(emailIntent,发送邮件));        打破;
    案例R.id.ibTakePic:        I =新意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);        startActivityForResult(ⅰ,cameraData);
        打破;
    }}

我如何进入这个图片,这样我可以把它添加到我的电子邮件意图是什么?我要对这个正确的方式?谢谢

编辑:这是我的全code

 公共类相机扩展活动实现View.OnClickListener {的ImageButton IB;
按钮B;
ImageView的IV;
我的意图;
最终静态INT cameraData = 0;
BMP位图;
文件MYIMAGE;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.photo);
    初始化();
    InputStream为= getResources()openRawResource(R.drawable.ic_launcher)。
    BMP = BitmapFactory.de codeStream(是);}私人无效初始化(){
    IB =(的ImageButton)findViewById(R.id.ibTakePic);
    B =(按钮)findViewById(R.id.bSendPic);
    IV =(ImageView的)findViewById(R.id.ivReturnedPic);
    b.setOnClickListener(本);
    ib.setOnClickListener(本);}@覆盖
公共无效的onClick(视图v){    // TODO自动生成方法存根
    开关(v.getId()){
    案例R.id.bSendPic:        如果(myImage.exists()){            串EMAILADDRESS [] = {info@sklep.com,,};            意图emailIntent =新意图(Intent.ACTION_SEND);
            emailIntent.putExtra(Intent.EXTRA_EMAIL,EMAILADDRESS);
            emailIntent.setType(图像/ JPEG);
            emailIntent
                    .putExtra(Intent.EXTRA_STREAM,Uri.fromFile(MYIMAGE));
            startActivity(Intent.createChooser(emailIntent,发送邮件));        }        打破;
    案例R.id.ibTakePic:        I =新意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);        startActivityForResult(ⅰ,cameraData);
        打破;
    }}保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,结果code,数据);
    如果(结果code == RESULT_OK){
        捆绑额外= data.getExtras();
        BMP =(位图)extras.get(数据);
        iv.setImageBitmap(BMP);        文件storagePath =新的文件(
                Environment.getExternalStorageDirectory()+/ DavePics /);
        storagePath.mkdirs();        MYIMAGE =新的文件(storagePath,Long.toString(系统
                .currentTimeMillis())+.JPG);        位图B = Bitmap.createScaledBitmap(BMP,320,480,FALSE);        尝试{
            FileOutputStream中出=新的FileOutputStream(MYIMAGE);
            b.com preSS(Bitmap.Com pressFormat.JPEG,80出);
            了out.flush();
            out.close();
        }赶上(例外五){
            e.printStackTrace();
        }    }
}}


解决方案

在code线,你就是忘记,,

  emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(MYIMAGE));

在您的code,申报文件MYIMAGE 在全球范围内的活动,

现在在电子邮件发送code

检查文件是否存在与否,

 如果(myImage.exist())
{
 串EMAILADDRESS [] = {info@sklep.com,,};
 意图emailIntent =新意图(Intent.ACTION_SEND);
 emailIntent.putExtra(Intent.EXTRA_EMAIL,EMAILADDRESS);
 emailIntent.setType(图像/ JPEG);
 emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(MYIMAGE));
 startActivity(Intent.createChooser(emailIntent,发送邮件));
}

I have an application that takes a picture and stores the picture internally in a folder that I have created. After taking a picture I want to be able to access it so that I can email it. How can I access the image I have just taken?Below is my code that saves the image internally after the picture has been taken:

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

    }
}

}

When I check the folder I have created the picture is there. What I want to do now is access that picture so that I can send it in an email from my code below:

@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;
    }

}

How do I access this pic so that I can add it to my email intent? Am i going about this the right way? Thanks

Edit: This is my full code

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

    }
}

}

解决方案

The code line you forgot is,,

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myImage));

In your code, declare File myImage globally in your activity,

Now at Email send code

check whether file is exist or not,

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

这篇关于如何访问存储在内部的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆