How to save app for startActivityForResult()? - android

I have an app, which requests an image with:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
Then user chooses an app from dialog with options "ALWAYS" and "JUST ONCE". The problem is that "ALWAYS" doesn't work. When startActivityForResult(intent, REQUEST_CODE); is called the second time, the dialog raises again. How should I handle this?
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK){
try {
InputStream stream = getContentResolver().openInputStream(
data.getData());
originalBitmap = BitmapFactory.decodeStream(stream);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Related

onbackpressed in show file chooser

private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
hey. i used that code for pick image from storage. but if i press the back button in my gadget. he is fc . how to give condition on canceled pick from storage..and not force close?
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST) {
filePath = data.getData();
if(filePath != null) {
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}else if(filePath == null){
startActivity(new Intent(this,HalamanUser.class));
}
} else if (requestCode == CAMERA_REQUEST) {
Log.i("hello", "REQUEST cALL");
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
Log.i("hello", "Exception" + e.getMessage());
}
}else {
startActivity(new Intent(this,HalamanUser.class));
}
}
When the user presses back, the result is RESULT_CANCELED, and the data that you receive is null. So the app crashes when you call data.getData(), as you are calling getData() on a null object. The are a few ways to get around this: you can check what the resultCode is, and make sure it's RESULT_OK. You can also simply check whether the data Intent is null before trying to get the data from it:
if (requestCode == PICK_IMAGE_REQUEST) {
if (data != null) {
filePath = data.getData();
} else {
// Note: if filePath is by default null, you don't need this else statement
filePath = null;
}
if (filePath != null) {
...

SAF - ACTION_CREATE_DOCUMENT - after I save the file on Drive the file is empty

I am a beginner in SAF. What I'm trying to do is super simple to save a config. Let's say the file is .conf.
I copy .conf to conf.txt and I save it on Drive.
Here is my code:
tools.deleteFile(dst); // delete conf.txt if it exists
int res = tools.copyFile(src,dst); // copy .conf to conf.txt
if(res == -1) return;
tools.viewFile(dst);
// verify in Log info that the content of cnf.txt is correct
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, dst);
startActivity(intent);
I do a save in Drive. The file appears on my pc but when I open it, it's empty.
When I do the inverse: ACTION_OPEN_DOCUMENT
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
Uri uri;
if (resultCode == Activity.RESULT_OK){
if (requestCode == 30){
if (resultData != null) {
uri = resultData.getData();
try {
String content =
readFile(uri);
} catch (IOException e) {
e.printStackTrace();
}
The function readFile opens the file and stops while reading because there is no data.
What did I do wrong?
The Intent(Intent.ACTION_CREATE_DOCUMENT) is for CREATING text file, and use onActivityResult() to get the uri (location) from the file, THEN you use OutputStream to WRITE data (byte[]) to the file.
private void createAndSaveFile() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, "testFileSam.txt");
startActivityForResult(intent, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
try {
Uri uri = data.getData();
OutputStream outputStream = getContentResolver().openOutputStream(uri);
outputStream.write("Hi, welcome to Sam's Android classroom! Have a good day!".getBytes());
outputStream.close();
Toast.makeText(this, "Write file successfully", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "Fail to write file", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "File not saved", Toast.LENGTH_SHORT).show();
}
}
}
You are just creating the document but not writing it.
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, dst);
startActivity(intent);
this will create document and return the uri of the document to your app.
Now you need to write something to this uri that you will get in onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
try {
Uri uri = data.getData();
OutputStream outputStream = getContentResolver().openOutputStream(uri);
outputStream.write("Hi, welcome to Sam's Android classroom! Have a good day!".getBytes());
outputStream.close(); // very important
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Android (API 26) take full picture

I use to get a picture this simple code, it work but the size of the pictures are too small.
How can i get from the camera a better quality pictures?
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == FragmentActivity.RESULT_OK) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
createImagePath();
Bitmap photo = (Bitmap) data.getExtras().get("data");
File file = new File(savedFileUri.getPath());
try {
FileOutputStream fos = new FileOutputStream(file);
photo.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
Log.e(TAG, "FileNotFoundException " + file.getAbsolutePath());
e.printStackTrace();
} catch (IOException e) {
Log.e(TAG, "IOException " + file.getAbsolutePath());
e.printStackTrace();
}
}
}
}
Thanks for help.

How to print an image in a new layout

i tried to print a image in a new layout but it doesn't work
someone can help me?
here the code:
in mainActivity:
case R.id.gallery: //in the moment that the user pressed the button pickAPic, the app will give him to choose a picture
startActivity(new Intent(MainActivity.this, Main2Activity.class));
Intent pickImageIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //the app ask access for the gallery
startActivityForResult(pickImageIntent, REQUEST_PICK_PIC); // when he press on a pic
break;
in main2activity:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case REQUEST_PICK_PIC:
if (resultCode == RESULT_OK) {
Uri imageUri = intent.getData();
Bitmap bitmap;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
iv.setImageBitmap(bitmap); // the app "prints" the image that the user choose
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
so what is my problem?
thanks you for help
You have to start your main2activity after handle the onActivityResult from your mainActivity like :
mainActivity:
case R.id.gallery: //in the moment that the user pressed the button pickAPic, the app will give him to choose a picture
Intent pickImageIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //the app ask access for the gallery
startActivityForResult(pickImageIntent, REQUEST_PICK_PIC); // when he press on a pic
break;
...
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case REQUEST_PICK_PIC:
if (resultCode == RESULT_OK) {
Uri imageUri = intent.getData();
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("IMAGE_URI", uri.toString());
startActivity(intent);
}
break;
}
}
in main2activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Uri uri = Uri.parse(getIntent().getStringExtra("IMAGE_URI"));
Bitmap bitmap;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
iv.setImageBitmap(bitmap); // the app "prints" the image that the user choose
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

How to upload image from gallery in android

I want to upload image from my phone gallery into my application .In my application there is button named upload. when i click button,it should move to gallery and in gallery if i select image that selected image should display as thumbnail in application.I want to upload 10 images, from gallery in my application.
On click of the gallery button, start startActivityForResult as follows:
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY);
Consequently, detect GET_FROM_GALLERY (which is a static int, any request number of your choice e.g., public static final int GET_FROM_GALLERY = 3;) inside onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Detects request codes
if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
To view gallery:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),REQUEST_CODE);
and to use it in your app:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch (requestCode) {
case REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
//data gives you the image uri. Try to convert that to bitmap
break;
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.e(TAG, "Selecting picture cancelled");
}
break;
}
} catch (Exception e) {
Log.e(TAG, "Exception in onActivityResult : " + e.getMessage());
}
}
This is the way to go:
startActivityForResult(
new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI
),
GET_FROM_GALLERY
);

Categories

Resources