我是 Android 新手,正在构建一个小应用程序,可以从相机拍照并将其保存到图库。
这里是捕获图像的函数。
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
这是activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp" >
<Button
android:id="@+id/btnSelectPhoto"
android:background="#149F82"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Photo" />
</LinearLayout>
</LinearLayout>
我想要做什么,当捕获图像时,我想在另一个 Activity (页面)上显示图像,而不是在具有捕获图像按钮的同一 Activity 上。如何做到这一点。
提前致谢
最佳答案
您只需从方法中传递新 Activity 的路径即可。
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("MyImagePath", destination.getAbsoluteFile());
startActivity(intent);
在新 Activity 中
File imgFile = new File(filePath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
关于android - 在新 Activity 中显示捕获的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35264908/