简体   繁体   English

将拍摄的图片从ImageView存储到手机存储中

[英]Store the taken picture from ImageView to the phone storage

After I take a picture I store the picture into ImageView, So if anyone has an idea or suggestion in how to store the picture after it shown on the ImageView into phone stage without user interaction Thanks in advance 拍照后,将图片存储到ImageView中,因此,如果有人对将ImageView中显示的图片存储到手机界面而不需要用户交互时有什么想法或建议,请先感谢

在此处输入图片说明

public class MainActivity extends Activity {
    ImageView viewpict;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpict=(ImageView) findViewById(R.id.pict_result);
        Button btn= (Button)findViewById(R.id.camera);

        btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            //  Intent intent = new Intent (getApplicationContext(),MainActivity2.class);

                //startActivity(intent);
                startActivityForResult(intent,0);

            }

     });


    }

protected void onActivityResult( int requestCode, int resultCode,Intent data)
{
    if (requestCode==0)
    {
        Bitmap theimage = (Bitmap) data.getExtras().get("data");
        viewpict.setImageBitmap(theimage);
    }

}

}

Try: 尝试:

viewpict.buildDrawingCache();
Bitmap bm=viewpict.getDrawingCache();

And save: 并保存:

 OutputStream fOut = null;
    Uri outputFileUri;
     try {
    File root = new File(Environment.getExternalStorageDirectory()
      + File.separator + "folder_name" + File.separator);
    root.mkdirs();
   File sdImageMainDirectory = new File(root, "myPicName.jpg");
    outputFileUri = Uri.fromFile(sdImageMainDirectory);
    fOut = new FileOutputStream(sdImageMainDirectory);
   } catch (Exception e) {
    Toast.makeText(this, "Error occured. Please try again later.",
      Toast.LENGTH_SHORT).show();
   }

   try {
    bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    fOut.flush();
    fOut.close();
   } catch (Exception e) {
   }

And permission in AndroidManifest: 以及AndroidManifest中的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM