Vishnu Handanakere Vishnu Handanakere - 3 months ago 11
Android Question

Android: Taking photo and saving that image in internal/external storage with specific name by programmitically?

I'm new to 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 programmitically??
When i click on save button it not storing in storage please help me
Here is my code:

Main Activity is

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 file is:

<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>


My Manifest file:

<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>

Answer

After Click Save button you call this method, Its 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();
        }
    }