Skip to content

Instantly share code, notes, and snippets.

@Mattamorphic
Created October 24, 2020 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mattamorphic/b99e1210deb48db6f0583d531a24fb4d to your computer and use it in GitHub Desktop.
Save Mattamorphic/b99e1210deb48db6f0583d531a24fb4d to your computer and use it in GitHub Desktop.
Example 034 (for API > 24)
<?xml version="1.0" encoding="utf-8"?>
<!-- Create in res/layouts/activity_main.xml -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_get_pic"
android:text="@string/btn_get_pic_text"
android:layout_alignParentTop="true" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv_view"
android:layout_below="@+id/btn_get_pic"
android:src="@drawable/ic_launcher_foreground"
android:contentDescription="@string/iv_view_text"
android:adjustViewBounds="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example034">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Example034">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:authorities="${applicationId}.provider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
package com.example.example034;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
private Button btn_get_pic;
private ImageView iv_view;
private Uri uri_new_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_get_pic = findViewById(R.id.btn_get_pic);
iv_view = findViewById(R.id.iv_view);
btn_get_pic.setOnClickListener((View view)-> {
// Launch intent for image capture
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// Grant the intent READ permissions
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Create a new file to store the image
File file_new = new File(MainActivity.this.getExternalFilesDir(null), "test.jpg");
// For target SDK > 24 we need to use FileProvider class to access file or folder and make then available
// After adding provider tag to application tag in manifest, use FileProvider.getUriForFile
uri_new_image = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file_new);
// Add extended data for the intent (in this case the URI for the file (content://)
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri_new_image);
// Start th intent, with code 100 (UID)
startActivityForResult(intent, 100);
});
}
@SuppressLint("LongLogTag")
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// When returning to this activity, handle the requestCode
if (requestCode != 100 || resultCode != RESULT_OK) return;
Log.v("MainActivity::onActivityResult", uri_new_image.toString());
// Using the uri_new_image Uri object decode this into a bitmap, and add to container
Bitmap image = decodeImageFile();
if (image == null) {
Log.d("MainActivity::onActivityResult", "failed to load image");
return;
}
iv_view.setImageBitmap(image);
}
/**
* Decodes the uri_file_new attribute into a Bitmap
* @return Bitmap
*/
private Bitmap decodeImageFile() {
Bitmap image = null;
// Provides access to the content model
ContentResolver content_resolver = getContentResolver();
try {
// Open an input stream using the new image URI
InputStream input_stream = content_resolver.openInputStream(uri_new_image);
// Decode the stream into an image
image = BitmapFactory.decodeStream(input_stream);
// Close the stream
input_stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
return image;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!-- Create in res/xml/proivder_paths.xml -->
<external-path name="external_files" path="." />
</paths>
<!-- Create in res/values/strings.xml -->
<resources>
<string name="app_name">example034</string>
<string name="btn_get_pic_text">Take a picture</string>
<string name="iv_view_text">Picture taken by camera</string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment