My Application is a simple CSV Viewer and i want to read File path when someone opens a CSV file using my App.
I tried below code but it's giving me error as -----------------------------------------------------------------------------.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String _file = intent.getData().toString();
Log.d("INFO",_file);
}
}
Sending Activity:
Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("imagePath", pathToImage);
startActivity(intent);
Receiving Activity:
String path = getIntent().getStringExtra("imagePath");
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("filePath", file.getAbsolutePath());
startActivity(intent);
in MyActivity.class
String path = getIntent().getStringExtra("filePath");
File file=new File(path);
Related
I'm trying to make facebook for "someone" else opens on application, not on a browser, for example National Geography https://www.facebook.com/natgeo
Here is the code I have:
ddc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/natgeo"));
startActivity(browserIntent);
}
});
I found one on stackoverflow and I tried it and it has errors, here is the code:
try {
//try to open page in facebook native app.
String uri = "fb://page/" + natgeo; //Custom URL
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
catch (ActivityNotFoundException ex){
//facebook native app isn't available, use browser.
String uri = "https://www.facebook.com/natgeo" + natgeo; //Normal URL
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uriMobile));
startActivity(i);
}
the thing is I want, if someone didn't have facebook App, it will open in the browser.
Edited
package fa;
import com.f.fa.R;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button ddc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
addListenerOnButton();
}
public void addListenerOnButton() {
ddc = (Button) findViewById(R.id.ddc);
try {
//try to open page in facebook native app.
String uri = "fb://page/" + natgeo; //Custom URL
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
catch (ActivityNotFoundException ex){
//facebook native app isn't available, use browser.
String uri = "https://www.facebook.com/natgeo" + natgeo; //Normal URL
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
}
}
}
You use a variable, which is not declared before (read compiler errors)..
Add String natgeo = "my content"; before your try-catch block to make your code compiling.
There is a strange behaviour of assigning/modifying instance variable of the class inside Button OnClick() handlers.
As shown below, the instance variable "CurrentFile" is modified inside imgBtn.setOnClickListener() handlers. But when this variable is accessed in onActivityResult() method, this variable contains the value "null".
According to my understanding, the activity "GetPicActivity" will not be destroyed till the Camera activity returns. So, the instance variable "CurrentFile" should not be null.
Please help, if I am missing some basics.
import java.io.File;
import java.io.IOException;
import com.favoritepics.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class GetPicActivity extends Activity {
protected static final int ID_REQ_IMAGE_CAPTURE = 1;
protected static final int ID_REQ_PICK_PHOTO = 0;
protected File currentFile = null; /// Goal is "to modify this variable inside Button onClick handlers"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.DKGRAY));
setContentView(R.layout.activity_get_pic);
// set handler image gallery
ImageButton imgBtn = (ImageButton) findViewById(R.id.imgGallery);
imgBtn.setOnClickListener(new ImageButton.OnClickListener() {
#Override
public void onClick(View v) {
// create intent to take picture on camera
Intent pickPhoto = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, ID_REQ_PICK_PHOTO);
}
});
// set handler for camera image
imgBtn = (ImageButton) findViewById(R.id.imgCamera);
imgBtn.setOnClickListener(new ImageButton.OnClickListener() {
#Override
public void onClick(View v) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File tempDir = Environment.getExternalStorageDirectory();
File tempFile = null;
try {
currentFile = File
.createTempFile("_jpeg_", ".jpg", tempDir);
} catch (IOException exception) {
Toast.makeText(
GetPicActivity.this,
"Problem occured during creation of temp file! "
+ exception.getMessage(),
Toast.LENGTH_SHORT).show();
}
if (currentFile != null) {
takePicture.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(currentFile));
startActivityForResult(takePicture, ID_REQ_IMAGE_CAPTURE);
}
}
});
// set handler for cancel button
Button btnCancel = (Button) findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case ID_REQ_IMAGE_CAPTURE:
if (resultCode == RESULT_OK) {
Intent newIntent = new Intent();
newIntent.putExtra("TYPE", requestCode);
newIntent.putExtra("PATH", currentFile.getAbsolutePath());
setResult(RESULT_OK, newIntent);
finish();
}
break;
default:
break;
}
}
}
click here to see Logcat messages
Steps what I intend to do:
1, Create a temporary file in External storage for the destination of Camera picture
2, Start Camera to take a picture by passing the URI of temporary file
3, OnActivityResult() from camera, return the (temporary) File path to previous activity for further process
But here what I see is,
As soon as the Camera is started & picture is taken, the GetPicActivity is created again. Because of this, the instance variable "CurrentFile" will be null & in OnActivityResult() , I could not get the File path of temporary file.
Why is the GetPicActivity destroyed after starting Camera?
When the camera is started, the Camera appears in the screen orientation "Landscape".
Even the orientation of already existing activity stack is changed to "Landscape" itseems.
So, When the result of Camera is returned to GetPicActivity method OnActivityResult(), the GetPicActivity is created again. But now, the instance variable "CurrentFile" is null. This, gives a NullPointerException.
i wan currently developing an app for android, but.. There is an activity of mine, in which the user shall click on a button and it would open the Android Terminal EMulator which would have been installed on the user device, but right now, i have an issue, my codes are not working.. check it out
ClockWorkModFlash.java
package com.loadedgeek.myupgrade;
import android.os.Bundle; import android.app.Activity; import
android.os.Handler; import android.content.Intent; import
android.widget.Button; import android.view.View;
public class ClockWorkModFlash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clockworkmod_activate);
Button bClock = (Button) findViewById(R.id.FlashClockWork);
bClock.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager managerclock = getPackageManager();
i = managerclock.getLaunchIntentForPackage(jackpal.androidterm);
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
}
});
}}
The Eclipse is not allowing me compile the app, as that is the last activity, it says Packagemanager and OnClickListner could not be resolved... any ideas?
Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(ComponentName.unflattenFromString("application Package name /application launcher activity name"));
intent.addCategory("android.intent.category.LAUNCHER");
startActivity(intent);
PackageManager managerclock = getPackageManager();
Intent i = managerclock.getLaunchIntentForPackage(jackpal.androidterm);
i.addAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
or
Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(ComponentName.unflattenFromString("jackpal.androidterm");
intent.addCategory("android.intent.category.LAUNCHER");
startActivity(intent);
by the way,your error "Packagemanager and OnClickListner could not be resolved", I guess that you can change the code from:
bClock.setOnClickListener(new OnClickListener() {…}
to:
bClock.setOnClickListener(new View.OnClickListener() {…}
I am updating code for a company app and there are about 20 activity classes that all download a PDF and then display it using this code:
public void showPdf()
{
File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}
the code is working, however it has been replicated in all 20 classes (seems very bad to me) and I would like to put it into a single class which each activity class imports, however when I try to do this, things like getPackageManager() and startActivity(intent) no longer work.
How can I structure my class to make this happen? or am I going about this the wrong way.
How can I structure my class to make this happen?
Step #1: Make this a static method on a utility class.
Step #2: Add Context ctxt as a parameter to the method.
Step #3: For methods like getPackageManager() and startActivity(), which are implemented on Context, call them on the passed-in ctxt parameter.
Step #4: Smack your wrist with a ruler for using string concatenation for creating a file path, and do it the right way.
Step #5: Get rid of the queryIntentActivities() code that you aren't using.
public static void showPdf(Context ctxt)
{
File file = new File(Environment.getExternalStorageDirectory(), "/pdf/Read.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);
}
Step #6: Have the places that are presently calling showPdf() call YourUtilityClass.showPdf(this).
public class PDFUtlity{
public static void showPdf(Context context)
{
File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
PackageManager packageManager = context.getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
((Activity)context).startActivity(intent);
}
}
public class MyActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
///.....
PDFUtlity.showPdf(this);
}
}
My code:
package elf.app;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import elf.app.entity.ELFList;
import elf.app.entity.Entry;
import elf.app.test.FakeComm;
// TODO Kunna skicka att något är färdigt (ett rum är städat).
public class RoomListActivity extends ListActivity {
private ELFList eList;
// private FakeComm fakecomm;
private Bundle extras;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.extras = getIntent().getExtras();
eList = new ELFList();
// fakecomm = new FakeComm();
// eList.add(fakecomm.getData());
String[] strArr = {"asd","sdf","dfg"};
eList.add(strArr);
String[] str = eList.returnNames();
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, str));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Entry e = eList.getEntry(position);
String roominfo = e.toString();
Intent intent = new Intent(this, RoomInfoActivity.class);
intent.putExtra("entry",roominfo);
this.startActivity(intent);
// old stuff
// String message;
// message = eList.getEntryInfo(position);
// Toast.makeText(getApplicationContext(),
// message, Toast.LENGTH_SHORT).show();
}
});
}
}
I'm getting errors at the following lines:
Intent intent = new Intent(this, RoomInfoActivity.class);
and
this.startActivity(intent);
I don't have much of a clue why I get these errors, the exact output in the editor for these errors are:
"The constructor Intent(new AdapterView.OnItemClickListener(){}, Class ) is undefined"
"The method startActivity(Intent) is undefined for the type new AdapterView.OnItemClickListener(){}"
I'm an Android newbie so please take that into consideration, however I have studied Java for about a year.
Fix
Intent intent = new Intent(this, RoomInfoActivity.class);
to
Intent intent = new Intent(RoomListActivity.this, RoomInfoActivity.class);
The error is because by this you refer to OnClickListener. The problem is fixed, if you refer to the Activity's this. The second error is the same - wrong reference. Just remove this, and startActivity() method will be searched within the enclosing class too.
try this
Intent intent = new Intent(RoomListActivity.this, RoomInfoActivity.class);
intent.putExtra("entry",roominfo);
RoomListActivity.this.startActivity(intent);