Here is the code, when selecting an image the app abruptly crashes :/ Please help, I cant progress further without this error being fixed.
Manifest
<activity
android:launchMode="singleTop"
android:name=".FoundMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.guruguru2.lostnfound.FOUNDMENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
ImageView XML
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.71"
android:src="#drawable/abc_list_divider_mtrl_alpha" />
.java file
Button pickImageButton = (Button)findViewById(R.id.pick_image_button);
private static final int PICK_IMAGE = 100;
private ImageView imageView2;
pickImageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() { //opens the gallery
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
imageView2.setImageURI(imageUri);
}
}
LogCat
E/AndroidRuntime(1083): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://media/external/images/media/16 }} to activity {com.guruguru2.lostnfound/com.guruguru2.lostnfound.FoundMenu}: java.lang.NullPointerException
The main goal here is to select an image from the gallery, and simply show it. I can post more logcat if needed, there is a lot more, this error seemed the most fatal though.
You might try to change the intent from INTERNAL_CONTENT_URI to EXTERNAL_CONTENT_URI.
Check this stackoverflow url for further information on this issue:
Android get image from gallery into ImageView
Here is the code, when selecting an image the app abruptly crashes
You forgot to set the view to the ImageView do it as follows :
imageView2 = (ImageView)findViewById(R.id.ImageView2);
Then you are able to set the image to your ImageView, it doesn't mean that it solves your problem, but it's the MAIN error in your code as I can see.
Related
I am trying to save a picture to external storage from a MediaStore.ACTION_IMAGE_CAPTURE intent.
I keep getting this error:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.media.action.IMAGE_CAPTURE dat=content://ca.kevincook.trackmytrip.fileprovider/DCIM/default_image.jpg flg=0x3 clip={text/uri-list U:content://ca.kevincook.trackmytrip.fileprovider/DCIM/default_image.jpg} (has extras) }
Here is my code:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagePath = new File(getContext().getExternalFilesDir(Environment.DIRECTORY_DCIM), "");
File newFile = new File(imagePath, "default_image.jpg");
Uri contentUri = FileProvider.getUriForFile(getContext(), "ca.kevincook.trackmytrip.fileprovider", newFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
intent.setData(contentUri);
getContext().grantUriPermission("ca.kevincook.trackmytrip", contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContext().grantUriPermission("ca.kevincook.trackmytrip", contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, 0);
onActivityResult method:
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0)
{
if (resultCode == Activity.RESULT_OK)
{
Uri uri = data.getData();
GalleryAdapter.addImage(BitmapFactory.decodeFile(uri.toString()));
}
}
SaveImagePopup("Enter Picture Name");
}
Activity declaration in manifest file:
<activity android:name=".MapsActivity" android:label="#string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Permissions in manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name = "android.hardware.camera"
android:required="true"/>
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_public_path" path="." />
</paths>
I copied the code exactly from the android developer website:
https://developer.android.com/reference/androidx/core/content/FileProvider
I used getContext() because I'm running it from a fragment. I also tried getActivity() in its place, but that didn't work.
Have you added the file provided in the manifest? If not Use it in this way
<provider
android:name=".fileprovider"
android:authorities="com.xxxx.xxxx.xxxx" //Your package name
android:enabled="true"
android:exported="false" />
and can handle in on the activity result
The code in onActivityResult will be like this,
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
File out = new File(getFilesDir(), "newImage.jpg");
if (!out.exists()) {
Toast.makeText(getBaseContext(), "Error while capturing image", Toast.LENGTH_LONG).show();
return;
}
Bitmap mBitmap = BitmapFactory.decodeFile(compressedImage.getAbsolutePath());
ivShowImage.setImageBitmap(mBitmap);
}
}
I have the following sequence of pages:
-MainActivity
-Intent(Select a picture)
-DadosActivity
In MainActivity I have a button to open an intent for the user to select a photo, with the following code:
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if (i.resolveActivity(MainActivity.this.getPackageManager()) != null) {
startActivityForResult(i, SELECAO_GALERIA);
}
In this intent, the user will select a photo and after this is sent that selected item to another page, through this code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == MainActivity.this.RESULT_OK) {
Bitmap image = null;
try {
switch (requestCode) {
case SELECAO_CAMERA:
image = (Bitmap) data.getExtras().get("data");
break;
case SELECAO_GALERIA:
Uri localImagemSelecionada = data.getData();
image = MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), localImagemSelecionada);
break;
}
if (image != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 70, baos);
byte[] dadosImagem = baos.toByteArray();
Intent i = new Intent(MainActivity.this, DadosPostagemActivity.class);
i.putExtra("fotoEscolhida", dadosImagem);
startActivity(i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
In the last activity, DadosActivity, the user can make some changes in the image, but my problem is when pressing the button on the back of the device and / or the toolbar, with the following code:
toolbar = (Toolbar) findViewById(R.id.tb_dados);
toolbar.setTitle("Adicione uma descrição");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
According to what I did on AndroidManifest
<activity
android:name=".activity.DadosPostagemActivity"
android:parentActivityName=".activity.MainActivity" />
Activity is returning to MainActivity, but I wanted to press it to return to the intent and to choose the image again.
Does anyone have any suggestions on how to do this, so that instead of returning to MainActivity, back to the intent so that he can choose the image again?
Use the Codes Below..
In Manifest.xml file change code of AddPhoto Activity and AddDescription Activity like this-
This is for Addphoto Activity
<activity android:name=".AddPhoto"
android:label="Add Photo"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
This is for AddDescription Activity
<activity android:name=".AddDescription"
android:label="Add Description"
android:parentActivityName=".AddPhoto">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".AddPhoto" />
</activity>
Now your problems are solved. In the code we declared that Addphoto activity is a child of Main Activity and AddDescription Activity is a child of Addphoto Acitivity.
Change the AddPhoto and AddDescription with your own activity name.
Best of Luck.
Thanks for everyone, I I found the solution was to just overwrite the method onBackPressed
I have two application A and B.
Application A
the following code I launch B Application for get Result from A application activity ..
String packageName = "com.cm.applicationb";
PackageManager manager = context.getPackageManager();
Intent i = manager.getLaunchIntentForPackage(packageName);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.putExtra("grand_total", "2500");
i.setAction(packageName);
startActivityForResult(i, REQUEST_DATA);
From Application A call onActivityResutl
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_DATA)
if (resultCode == Activity.RESULT_OK) {
me=data.getStringExtra("Obj");
}
Toast.makeText(getApplicationContext(),me,Toast.LENGTH_LONG).show();
}
B Application setResult Data
Intent i=new Intent();
i.putExtra("Obj", "object");
setResult(Activity.RESULT_OK, i);
finish();
Ever retrun null data and resultcode from onActivityResult. How we can solve it? It is impossible get data onAcitivityResult from another applicaion setREsult. Anyone please help me for us. Thank you so much.
Remove finish() and call like this in B:
#Override
public void onBackPressed() {
Intent data = new Intent();
Bundle bundle = new Bundle();
bundle.putString("Obj", "Obj_Data");
data.putExtras(bundle);
setResult(Activity.RESULT_OK, data);
super.onBackPressed(); // this calls finish(); internally.
}
just add this line to your manifest.xml
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
for more detail, check this links that explain about how to receive data from other application
I've been trying to figure out how to solve this but was unsuccessful.
The problem here is after clicking the login button, it loads the Facebook Activity normally, and when it finishes, my activity receives the activity result and notifies the CallbackManager (all as described in the docs).
Unfortunately nothing happens from this point, no method from the registered FacebookCallback is executed, and not even a line is logged to inform of an error.
Here are the involved files:
public class LoginActivity extends Activity
CallbackManager callbackManager;
LoginButton loginFacebook;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this);
setContentView(R.layout.activity_login);
...
callbackManager = CallbackManager.Factory.create();
loginFacebook = (LoginButton) findViewById(R.id.login_button);
loginFacebook.setReadPermissions("public_profile","email");
loginFacebook.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
...
(This is never executed)
}
#Override
public void onCancel() {
...
(Not executed)
}
#Override
public void onError(FacebookException e) {
...
(Not executed either)
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(resultCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
...
}
And activity_login.xml includes the button like this
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"/>
I also added the Facebook Activity and APP ID in my android manifest file
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/facebook_app_id" />
As well as the android app key in the facebook application.
Hope figures out a solution for this, thank you very much.
Ok I see now. This is probably wrong
callbackManager.onActivityResult(resultCode, resultCode, data);
See resultCode is entered twice. What you want to do is:
callbackManager.onActivityResult(requestCode, resultCode, data);
Try adding this in your manifest.xml between application
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar" /><activity
In my app, I model Lists of items. In MainActivity, I see ListView containing Lists. I can click on each list to see its items (DisplayListActivity). On DisplayListActivity, I have button in the ActionBar to display the list properties. This launches the third activity (EditListPropertiesActivity).
Main
| ^
V |
DisplayListActivity (listId is passed with Intent, default=1)
| ^
V |
EditListPropertiesActivity (listId is passed with Intent, default=1)
The problem appears, when I select List id=2 on the MainActivity, and then I select the properties button on the DisplayListActivity. Once I am done with the EditListPropertiesActivity, i click '<' (back) on the ActionBar: .
I return to the DisplayListActivity, but instead of going back to the list id=2, I see the list with id=1 (which is default).
How to pass the ListId back form EditListPropertiesActivity to the DisplayListActivity?
startActivityForResult and return the id - would work, but I see it ugly
use the SharedPreferences and store sth like lastVisitedList - ugly
store the lastVisitedList information in the db - even uglier
What is the usual solution for that problem?
Code snippets below.
MainActivity
public class MainActivity extends Activity {
...
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
MetaList mlist = (MetaList) listView.getItemAtPosition(i);
final Intent intent;
intent = new Intent(getApplicationContext(), DisplayListActivity.class);
intent.putExtra(META_LIST_SELECTED, mlist.getId());
startActivity(intent);
}
}
);
...
DisplayListActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list);
intent = getIntent();
metaListId = intent.getLongExtra(MainActivity.META_LIST_SELECTED, 1); //read the data or take 1 as default
...
//start the third activity
final Intent intent;
intent = new Intent(getApplicationContext(), EditListPropertiesActivity.class);
intent.putExtra(META_LIST_SELECTED, metaListId);
startActivity(intent);
....
EditListPropertiesActivity
public class EditListPropertiesActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_list_parameters);
getActionBar().setDisplayHomeAsUpEnabled(true); //this enables the '<' (back) button
intent = getIntent();
metaListId = intent.getLongExtra(DisplayMetaListActivity.META_LIST_SELECTED, 1);
...
Manifest
<application>
<activity
android:name=".MainActivity">
</activity>
<activity
android:name="com.example.tutorial1.DisplayListActivity"
android:parentActivityName="com.example.tutorial1.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tutorial1.MainActivity" />
</activity>
<activity
android:name="com.example.tutorial1.EditListPropertiesActivity"
android:parentActivityName="com.example.tutorial1.DisplayListActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tutorial1.DisplayListActivity" />
</activity>
</application>
try this to finish an activity
Intent intent = new Intent();
intent.putExtra(EXTRA, value);
setResult(RESULT_OK, output);
finish();
and this for getting the result in the previous activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data.getExtras().containsKey(EXTRA)){
// do stuff with data from finished activity
String bla = data.getStringExtra(EXTRA)
}
}
EDIT: read comments! need to use startActivityForResult
try to call finish(); that you should call on destroy of the current activity and display the previous activity.
I have found the solution that works. I post it here, because it is cleaner (in my opinion) and different than the other proposed. I set the launch mode of the second activity to singleTask in the manifest file: android:launchMode="singleTask"
...
<activity
android:name="com.example.tutorial1.DisplayListActivity"
android:label="Meta List"
android:parentActivityName="com.example.tutorial1.MainActivity"
android:launchMode="singleTask">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tutorial1.MainActivity" />
</activity>
...
If there is a better solution for the stated problem, I am open for discussion :)