gpt4 book ai didi

java - Android Studio : How to set an external file(photo from the camera) to an imageview

转载 作者:行者123 更新时间:2023-12-02 13:15:15 24 4
gpt4 key购买 nike

不知道如何将 ImageView 设置为我刚刚用相机拍摄的图片。如果有某种方法可以同时显示多张捕获的图片,那就太好了。每当我单击该按钮时,就会出现之前拍摄的图像,然后打开相机,这是不对的。我希望 ImageView 为空白,我单击按钮,拍照,然后该照片显示在 ImageView 中。我认为这条线不合适,但我不确定如何/在哪里移动它。 mimageView.setImageURI(outputFileUri);

public class cameraclass extends Activity {
int TAKE_PHOTO_CODE = 0;
public static int count = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
final ImageView mimageView;
mimageView = (ImageView) this.findViewById(R.id.image_from_camera);




// Here, we are making a folder named picFolder to store
// pics taken by the camera using this application.
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();

Button capture = (Button) findViewById(R.id.take_image_from_camera);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

// Here, the counter will be incremented each time, and the
// picture taken by camera will be stored as 1.jpg,2.jpg
// and likewise.
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
}
catch (IOException e)
{
}

//Uri outputFileUri = Uri.fromFile(newfile);
Uri outputFileUri = FileProvider.getUriForFile(getApplicationContext() , "com.example.android.com220finalapp.provider", newfile);

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
mimageView.setImageURI(outputFileUri);

}
});
}

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

if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");

}
}
}

最佳答案

I believe that this line is out of place, but i'm unsure as to how/ where to move it.

startActivityForResult() 是异步的。该方法返回时,您的照片尚未拍摄。如果您收到 RESULT_OK 响应,则需要在 onActivityResult() 中将图像加载到 ImageView 中。

但是,虽然 setImageURI() 可能有效,但这从来都不是一个特别好的主意,因为它会在加载照片时卡住您的应用一段时间。有很多image loading libraries for Android它将处理异步加载您的ImageView

关于java - Android Studio : How to set an external file(photo from the camera) to an imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43815000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com