How to access an image that is stored internally

丶灬走出姿态 提交于 2020-01-05 19:38:09

问题


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

    }
}

}

回答1:


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


来源:https://stackoverflow.com/questions/12179045/how-to-access-an-image-that-is-stored-internally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!