Ich arbeite auf der Kameraseite. Android: Foto machen und dieses Bild im internen/externen Speicher mit spezifischem Namen des Gerätes programmgesteuert speichern? Wenn ich Schaltfläche Speichern mein Code klicken Sie darauf nicht in speicherung bitte helfen Sie mir hier:Android: Foto machen und dieses Bild im internen/externen Speicher mit spezifischem Namen programmgesteuert speichern?
Hauptaktivität ist
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-Datei ist:
<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>
Meine Manifest-Datei:
<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>
Stehen Sie vor einem Fehler? – RajSharma
Nein, ich sehe keinen Fehler, aber es erstellt keinen Ordner im internen/externen Speicher und speichert das angeklickte Bild nicht. –
Mit welcher Geräte-/Betriebssystemversion haben Sie das getestet? –