Android:通过编程拍摄照片并将该图像以特定名称保存在内部/外部存储中吗? [英] Android: Taking photo and saving that image in internal/external storage with specific name by programmatically?
本文介绍了Android:通过编程拍摄照片并将该图像以特定名称保存在内部/外部存储中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在相机方面工作. Android:通过编程拍摄照片并将该图像以设备的特定名称保存在内部/外部存储中吗? 当我单击保存"按钮时,该按钮未存储在存储中,请帮助我 这是我的代码:
I'm working on Camera side. Android: Taking photo and saving that image in internal/external storage with specific name of the device by programmatically? When i click on save button it not storing in storage please help me Here is my code:
主要活动是
public class MainActivity extends AppCompatActivity {
Button cButton, cSavaBtn;
ImageView ivCamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cButton = (Button) findViewById(R.id.button);
cSavaBtn = (Button) findViewById(R.id.buttonSave);
ivCamera = (ImageView) findViewById(R.id.imageView);
cButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplication(), "Camera Clicked", Toast.LENGTH_LONG);
Intent cIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cIntent, 0);
}
});
cSavaBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplication(), "Save btn Clicked", Toast.LENGTH_LONG);
AfterSaveClick();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data ){
super.onActivityResult(requestCode,resultCode,data);
Bitmap bm = (Bitmap) data.getExtras().get("data");
ivCamera.setImageBitmap(bm);
}
public void AfterSaveClick() {
ivCamera.buildDrawingCache();
Bitmap bm = ivCamera.getDrawingCache();
OutputStream fOut = null;
try {
Toast.makeText(this, "Correct now",
Toast.LENGTH_SHORT).show();
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "sharathFolder" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
fOut = new FileOutputStream(sdImageMainDirectory);
Toast.makeText(this, "Correct end now",
Toast.LENGTH_SHORT).show();
} 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) {
Toast.makeText(this, "Exception.",
Toast.LENGTH_SHORT).show();
}
}
}
XML文件为:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:text="Camera API"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@mipmap/ic_launcher"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="camera"
android:id="@+id/button"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAVE"
android:id="@+id/buttonSave"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp" />
</RelativeLayout>
我的清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kgsharat"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.WRITE_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">
android:screenOrientation="portrait"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
推荐答案
单击保存"按钮后,必须调用此方法.对我来说很好
After Clicking Save button, you have to call this method. It works fine for me
public void AfterSaveClick() {
ivCamera.buildDrawingCache();
Bitmap bm = ivCamera.getDrawingCache();
OutputStream fOut = null;
try {
Toast.makeText(this, "Correct now",
Toast.LENGTH_SHORT).show();
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "sharathFolder" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
fOut = new FileOutputStream(sdImageMainDirectory);
Toast.makeText(this, "Correct end now",
Toast.LENGTH_SHORT).show();
} 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) {
Toast.makeText(this, "Exception.",
Toast.LENGTH_SHORT).show();
}
}
这篇关于Android:通过编程拍摄照片并将该图像以特定名称保存在内部/外部存储中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文