I Just Want To pass High-quality images from one activity to another
I Don't know How to Fix It. Please help ME
This Is My Main Activity
I Do this Form for Two weeks. But I am failed. I need Some Help and guidance. To pass this iamge
package com.maymaps.picturestudio;
import static android.provider.AlarmClock.EXTRA_MESSAGE;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileObserver;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private AdView mAdView;
private Button button1, button2, button3;
static final int REQUEST_IMAGE_CAPTURE = 1;
public static final int PICK_IMAGE = 2;
String currentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" timeStamp "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().setStatusBarColor(getResources().getColor(R.color.Blue, this.getTheme()));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(getResources().getColor(R.color.Blue));
}
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
AdSize adSize = new AdSize(300, 50);
mAdView.loadAd(adRequest);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.button1) {
openGallery();
}
if (view.getId() == R.id.button2) {
openCamera();
}
if (view.getId() == R.id.button3) {
}
}
private void openGallery() {
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
startActivityForResult(chooserIntent, PICK_IMAGE);
}
private void openCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File...
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
protected void onActivityResult(int reqCode, int resCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(reqCode, resCode, data);
if (resCode == RESULT_OK) {
if (reqCode == REQUEST_IMAGE_CAPTURE) {
if (data != null) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
//Starting activity (ImageViewActivity in my code) to preview image
Intent intent = new Intent(this, result.class);
intent.putExtra("BitmapImage", photo);
startActivity(intent);
}
} else if (reqCode == PICK_IMAGE) {
if (data.getData() != null) {
Uri imageUri = data.getData();
//Starting activity (ImageViewActivity in my code) to preview image
Intent intent = new Intent(this, result.class);
intent.putExtra("ImageUri", imageUri.toString());
startActivity(intent);
}
}
}
}
}
Receive Activity
package com.maymaps.picturestudio;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
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.widget.ImageView;
public class result extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
imageView=findViewById(R.id.imageView3);
Uri selectedImgUri = getIntent().getData();
String[] selectedImgPath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImgUri,
selectedImgPath, null, null, null);
cursor.moveToFirst();
int indexCol = cursor.getColumnIndex(selectedImgPath[0]);
String imgPath = cursor.getString(indexCol);
cursor.close();
imageView.setDrawingCacheEnabled(true);
imageView.setImageBitmap(BitmapFactory.decodeFile(imgPath));
/* Getting Image from Camera from Main Activity */
imageView.setDrawingCacheEnabled(true);
Uri CapturedImgUri = getIntent().getParcelableExtra("BitmapImage");
imageView.setImageURI(CapturedImgUri);
}
}
My Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.maymaps.picturestudio">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PictureStudio">
<activity
android:name=".result"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="false" />
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
<activity
android:name=".Splash_Screen"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
I Don't Know How To solve This. If Any talented People Know This Please Help Me
CodePudding user response:
This can be solved using the following design pattern; You can have a singleton class that can also the assignment of this image from whatever activity or fragment of your choice.
For example; let this be your singleton
public class ActivityHolder {
private static ActivityHolder instance = null;
private ActivityHolder() {
}
public static ActivityHolder getInstance() {
if (instance == null) {
instance = new ActivityHolder();
}
return instance;
}
public ImageView imageView;
}
You can use the above class as follows;
Assuming you want to access the image content in the first activity i.e MainActivity.
ActivityHolder.getInstance().imageView = "some image object here"
Then in your resulting activity, you can access this image by calling the sigleton image view. So your image assignment might look like ; ImageView imageView = ActivityHolder.getInstance().imageView
CodePudding user response:
You can use the "putExtra" function of the intent to pass the value you insert in the "findViewByID" function into the other activity.