如何从外部存储中保存照片并将其设置为ImageView

时间:2018-03-11 13:34:07

标签: java android

JAVA CODE

public class MainActivity extends AppCompatActivity {

TextView mTextView;
Button mButton;
ImageView mImageView;
private File imageFile;

private static final int REQUEST_CODE=123;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextView= (TextView) findViewById(R.id.idTextView);
    mButton= (Button) findViewById(R.id.idButton);
    mImageView= (ImageView) findViewById(R.id.idImageView);

    Typeface mTypeFace=Typeface.createFromAsset(getAssets(),"fonts/contra-italic.ttf");
    mTextView.setTypeface(mTypeFace);

    //String path=Environment.getExternalStorageDirectory()+"/nabeel.jpg";

   File file=new File(Environment.getExternalStorageDirectory()+"/nabeel.jpg");
    if (file.exists())
    {
        mImageView.setImageURI(Uri.fromFile(file));

    }


    Toast.makeText(this,file.getAbsolutePath(),Toast.LENGTH_LONG).show();


    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            imageFile= new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"nabeel.jpg");
            Uri uri=Uri.fromFile(imageFile);

            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivityForResult(intent,REQUEST_CODE);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==REQUEST_CODE){

        switch (resultCode){

            case AppCompatActivity.RESULT_OK:
                Toast.makeText(this,"File stored at " +imageFile.getAbsolutePath(),Toast.LENGTH_LONG).show();
                break;
            case AppCompatActivity.RESULT_CANCELED:
                Toast.makeText(this,"File not stired Properly",Toast.LENGTH_LONG).show();
                break;
        }
    }
}

}

问题

从上面的代码我可以使用按钮拍照 保存到外部存储,但我想显示 一旦保存了ImageView的图像.... 试过

仍然无法正常工作且无法在imageview中显示图像 如何尽快使用imageview显示捕获的图像 它会保存到外部存储

使用毕加索图书馆

我已经编码了这样的东西,但仍然无法使图像可见

      File file=new 
      File(Environment.getExternalStorageDirectory()
      .getAbsolutePath()+"/nabeel.jpg");

      Toast.makeText(MainActivity.this,file.getAbsolutePath(),
      Toast.LENGTH_LONG).show();

    Picasso.with(this).load(file.getAbsolutePath()).into(mImageView);

toast消息显示图像的确切路径,但不在图像视图中显示如何使其工作

问题2

我注意到当我使用下面的行来获取路径时我没有显示图像

   preview_Picture_File_directory=
    Environment.getExternalStorageDirectory().getAbsolutePath();

但是你使用代码获取路径并且它也显示图像

   preview_Picture_File_directory = 
   getExternalFilesDir("images").getAbsolutePath();

这两者之间的差异是什么?

问题3

正如我已经注意到" getExternalFilesDir "这个命令会将文件存储在Android / data / packageName / image.jpg中,这里的问题是当应用程序未被保存的文件和放大器时。图片全部删除。

另一方面" Environment.GetExternalpublicstorageDirectory()"这个命令是公共目录中的图像,我可以在图库中看到它,并且在卸载应用程序时它不会被删除。我的问题是我无法获取我保存在图片中的图像使用此命令的公共目录(Environment.GetExternalpublicstorageDirectory())

Java代码

public class MainActivity extends AppCompatActivity {


ImageView mImageView;
Button mButton;
File image_file;
String file_path;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mImageView= (ImageView) findViewById(R.id.idImageView);
    mButton= (Button) findViewById(R.id.idButton);

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            file_path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath();
            image_file=new File(file_path,"nab.jpg");
            Uri uri_file=Uri.fromFile(image_file);

            Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,uri_file);
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
            startActivityForResult(intent,1);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode==1){

        switch (resultCode){

            case AppCompatActivity.RESULT_OK:



                Uri file=Uri.fromFile(image_file);
                Bitmap b=BitmapFactory.decodeFile(file.getPath());
                mImageView.setImageBitmap(b);

                break;

            case AppCompatActivity.RESULT_CANCELED:
                break;
        }
    }
}

0 个答案:

没有答案