Android 安卓:如何在Webview/Imageview afterword中拍摄并观看?
在我的应用程序中,我正在拍摄一张照片,并使用内置的Android 安卓:如何在Webview/Imageview afterword中拍摄并观看?,android,webview,imageview,Android,Webview,Imageview,在我的应用程序中,我正在拍摄一张照片,并使用内置的摄像头应用程序保存它。然后,拍照后,我的应用程序返回到空白的活动。所有这些都有效 现在,我不想保存并返回空的活动,而是想在网络视图或图像视图上查看拍摄的照片。如何操作 我从MainActivity类启动CameraAdapter类,如下所示: public void takePicture(View view) { Intent cameraIntent = new Intent(this, CameraAdapter.class
摄像头
应用程序保存它。然后,拍照后,我的应用程序返回到空白的活动
。所有这些都有效
现在,我不想保存并返回空的活动
,而是想在网络视图
或图像视图
上查看拍摄的照片。如何操作
我从MainActivity
类启动CameraAdapter
类,如下所示:
public void takePicture(View view) {
Intent cameraIntent = new Intent(this, CameraAdapter.class);
startActivity(cameraIntent);
}
。。下面是CameraAdapter
类:
public class CameraAdapter extends Activity {
static Uri imageUri;
static File imageFolderPath;
int TAKE_PHOTO_CODE = 0;
String imageFileName;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imgs);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getUri());
startActivityForResult(intent, TAKE_PHOTO_CODE);
ImageView imageV = (ImageView) findViewById(imageView);
String filename = getImageFileName();
Bitmap image = getImage(filename);
imageV.setImageBitmap(image);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(),
"Capture is saved to:\n" + imageFolderPath.toString(),
Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(),
"You cancelled the capture",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
"Capture failed",
Toast.LENGTH_SHORT).show();
}
}
public Uri getUri(){
imageFolderPath = new File(Environment.getExternalStorageDirectory() + "/mst_imgs");
if(!imageFolderPath.exists())
imageFolderPath.mkdir();
String imageFullName = imageFolderPath + "/" + getImageFileName();
File newPic = new File(imageFullName);
imageUri = Uri.fromFile(newPic);
return imageUri;
}
private Bitmap getImage(String imageFileName) {
File root = Environment.getExternalStorageDirectory();
//return BitmapFactory.decodeFile(root + "/mst_imgs/" + imageFileName);
return BitmapFactory.decodeFile(root + "/mst_imgs/CS_TEST.jpg");
}
private String getImageFileName(){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'_'HHmmss");
String timeStamp = dateFormat.format(new Date());
//imageFileName = "CS_" +timeStamp + ".jpg";
imageFileName = "CS_TEST.jpg";
return imageFileName;
}
更新 我稍微修改了代码: 此外,以下是
imgs
xml布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/imgs_webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/btnShareImage"
android:text="Share ->"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
谢谢您现在已将文件保存到设备中,太好了,您现在要做的只是将其设置为图像视图 如果使用位图,则需要使用BitmapFactory并执行一些其他操作,以获取文件并将其转换为可以在imageview上设置的内容,然后担心加载正确的大小等等 您可以使用大量库来处理此问题。但我想说的是,你最好的选择是直接使用——你可以给它一个文件,它会加载并显示它。我用这个,它让生活变得更简单
Picasso.with(context).load(new File(fileName)).into(imageView);
如果文件路径不是直接加载文件,也可以使用它
希望这有帮助,多做一些谷歌搜索吧!:) 您现在已将文件保存到设备中,太好了,您现在要做的就是将其设置为图像视图 如果使用位图,则需要使用BitmapFactory并执行一些其他操作,以获取文件并将其转换为可以在imageview上设置的内容,然后担心加载正确的大小等等 您可以使用大量库来处理此问题。但我想说的是,你最好的选择是直接使用——你可以给它一个文件,它会加载并显示它。我用这个,它让生活变得更简单
Picasso.with(context).load(new File(fileName)).into(imageView);
如果文件路径不是直接加载文件,也可以使用它
希望这有帮助,多做一些谷歌搜索吧!:) 您需要使用您的
intent extra
,将文件名返回到预览活动,并使用时间戳和将文件解码为位图,如下所示:
private Bitmap getImage(String imageFileName) {
File root = Environment.getExternalStorageDirectory();
return BitmapFactory.decodeFile(root + "/mst_imgs/" + imageFileName);
}
在layout.xml
中创建一个ImageView
。在onCreate()中插入:
不要忘记将读取文件的访问权限添加到Manifest.xml
:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
活动\u main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/imgs_webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/btnShareImage"
android:text="Share ->"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rodrigo.teste">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Manifest.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/imgs_webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/btnShareImage"
android:text="Share ->"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rodrigo.teste">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
您需要返回文件名以预览活动,使用您的intent extra
,并使用时间戳和将文件解码为位图,如下所示:
private Bitmap getImage(String imageFileName) {
File root = Environment.getExternalStorageDirectory();
return BitmapFactory.decodeFile(root + "/mst_imgs/" + imageFileName);
}
在layout.xml
中创建一个ImageView
。在onCreate()中插入:
不要忘记将读取文件的访问权限添加到Manifest.xml
:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
活动\u main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/imgs_webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/btnShareImage"
android:text="Share ->"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rodrigo.teste">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Manifest.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/imgs_webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/btnShareImage"
android:text="Share ->"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rodrigo.teste">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
不客气。仅在测试中,您可以尝试更改“CS_u”+timeStamp+“.jpg”代码>到一个固定的名称,如“CS_TEST.jpg”代码>。用于解码文件(root+“/mst_imgs/CS_TEST.jpg”)代码>.humm。。。。不确定。您能否将要显示图片的活动代码放入问题中?如果可能,也包括您的layout.xml
。你相信如果我告诉你这对我有用吗=让我们确认一些值。TAKE\u PHOTO\u code
是否等于1(int)?除了读取权限外,在清单中确认权限``。要确认,是否需要自动显示图片?我是按按钮得到的。如果您需要修复它,最好创建另一个活动,只显示图片。请查看我的更新答案,我正在通过代码测试。欢迎您。仅在测试中,您可以尝试更改“CS_u”+timeStamp+“.jpg”代码>到一个固定的名称,如“CS_TEST.jpg”代码>。用于解码文件(root+“/mst_imgs/CS_TEST.jpg”)代码>.humm。。。。不确定。您能否将要显示图片的活动代码放入问题中?如果可能,也包括您的layout.xml
。你相信如果我告诉你这对我有用吗=让我们确认一些值。TAKE\u PHOTO\u code
是否等于1(int)?除了读取权限外,在清单中确认权限``。要确认,是否需要自动显示图片?我是按按钮得到的。如果您需要修复它,最好创建另一个活动,只显示图片。请查看我的更新答案,我正在通过代码测试。谢谢您的回答。但是我有一个问题,我没有(或者我找不到)build.gradle文件来添加库!!编译'com.squareup.picasso:picasso:2.5.2'只是一个提示-如果你在android studio中,你可以转到'Module Settings',然后转到'Dependency'选项卡,然后单击绿色“+”,然后选择'Library Dependency'。这将使您进入一个屏幕,在那里您可以有效地搜索依赖项。请记住,获取这些信息的最佳方式是从网站本身获取,但如果您知道需要哪一个,但不知道完整的声明,请尝试此“我在Android Studio上”。但是当我打开项目结构时(为了打开模块设置
,它会显示“无显示符号”。当我点击“+”
符号时,我看不到库依赖关系
!!有任何建议吗?!点击“应用程序”文件夹,您应该只能够点击F4,然后转到依赖项选项卡-如果没有“应用”文件夹/模块,您的gradle可能有点错误,没什么大不了的