I am trying to develop an application where I want to pick only .xls/.xlsx file and I want to read the data from it..how should I achieve it?
I wrote code below ..but it allows me to pick up all type of data.`
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnpurchase=(Button)findViewById(R.id.btnpurchase);
btnsales=(Button)findViewById(R.id.btnsales);
btnpurchase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/xlsx");
startActivityForResult(intent, 7);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch(requestCode){
case 7:
if(resultCode==RESULT_OK){
String PathHolder = data.getData().getPath();
Toast.makeText(MainActivity.this, PathHolder , Toast.LENGTH_LONG).show();
}
break;
}
}
You can refer a list of Microsoft Office MIME from here https://stackoverflow.com/a/4212908/3283376
.xls file
application/vnd.ms-excel
.xlsx file
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
I think they are that what you want.
Related
How do I take the 'this' value ( from the 6th line) and make it into a string?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrscanner);
fragment = (BarcodeFragment)getSupportFragmentManager().findFragmentById(R.id.sample);
fragment.setScanResultHandler(this);
btn = ((Button)findViewById(R.id.scan));
btn.setEnabled(false);
}
For example instead of just toasting the value, can I use the value and make it into a string and then carrying it into another activity.
#Override
public void scanResult(ScanResult result) {
btn.setEnabled(true);
Intent intent = new Intent(QrscannerActivity.this,ProductInfoActivity.class);
startActivity(intent);
Toast.makeText(this, result.getRawResult().getText(), Toast.LENGTH_LONG).show();
}
You should use the Google ZXing Barcode Scanner library and with the below code:
extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HandleClick hc = new HandleClick();
findViewById(R.id.butQR).setOnClickListener(hc);
findViewById(R.id.butProd).setOnClickListener(hc);
findViewById(R.id.butOther).setOnClickListener(hc);
}
private class HandleClick implements OnClickListener{
public void onClick(View arg0) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
switch(arg0.getId()){
case R.id.butQR:
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
break;
case R.id.butProd:
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
break;
case R.id.butOther:
intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR");
break;
}
startActivityForResult(intent, 0); //Barcode Scanner to scan for us
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
TextView tvStatus=(TextView)findViewById(R.id.tvStatus);
TextView tvResult=(TextView)findViewById(R.id.tvResult);
if (resultCode == RESULT_OK) {
tvStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
} else if (resultCode == RESULT_CANCELED) {
tvStatus.setText("Press a button to start a scan.");
tvResult.setText("Scan cancelled.");
}
}
}
}
In my app I create two activities and I want to get input from the second activity and use it in the first activity, I use startActivityForResult but there is a problem in it!
That is the code of first activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final Button b=(Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent("net.naif.action.GETDATA");
startActivityForResult(i,77);
}
});
}
protected void onActivityForResult (int requestCode,int resultCode,Intent data){
if (requestCode ==77 && resultCode ==RESULT_OK){
String msg =data.getStringExtra("text");
Toast.makeText(getBaseContext(),msg,Toast.LENGTH_SHORT).show();
}
}
And this for the second:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
final Button b=(Button)findViewById(R.id.button);
final EditText t=(EditText)findViewById(R.id.editText);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s=t.getText().toString();
Intent i=new Intent();
i.putExtra("text",s);
setResult(Activity.RESULT_OK,i);
finish();
}
});
}
Android studio notify me that is the method ( protected void onActivityForResult (int requestCode, int resultCode, Intent data) is never used.
That's because the method is named onActivityResult, not onActivityForResult.
My UI has a button and a videoview.When we click button,go to Gallery Video and when we select a video in Video gallery,it return my UI and videoview will display video that is selected..I used code as follow but it don't display video :(
public class VideoGalleryActivity extends Activity {
/** Called when the activity is first created. */
Button button;
VideoView videoView;
private static final int PICK_FROM_GALLERY=1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
videoView=(VideoView)findViewById(R.id.videoview);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"),PICK_FROM_GALLERY);
}
}) ;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK) return;
if (requestCode == PICK_FROM_GALLERY) {
Uri mVideoURI = data.getData();
videoView.setVideoURI(mVideoURI);
}
}
You have to let videoview start by adding videoview.start();. Code:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK) return;
if (requestCode == PICK_FROM_GALLERY) {
Uri mVideoURI = data.getData();
videoView.setVideoURI(mVideoURI);
videoview.start(); //edited
}
}
I am making an android application, which takes an image from camera and then displays it. However, I am unable to display the clicked image probably because the onActivityResult() is not triggered.
Here is my piece of code. Can anyone suggest me what am i missing ?
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final int CAMERA_PIC_REQUEST = 1337;
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
#override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("Message1", "I reached 2");
//super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST) {
// do something
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(thumbnail);
}
}
});
}
}
onActivityResult() must be declared in your Activity class (not inside the onClickListener). If you correct the "#override" ('o' must be capitalized), typo before your current onActivityResult() declaration, you'll see what I mean...
See the Activity.onActivityResult() documentation.
Here's how your class should look like:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final int CAMERA_PIC_REQUEST = 1337;
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("Message1", "I reached 2");
//super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST) {
// do something
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(thumbnail);
}
}
}
I'm new in android and working on an app that captures photo by camera and set it as wallpaper.Here's the code :
public class camera extends Activity implements View.OnClickListener {
private ImageButton imgb;
private ImageView imgv;
private Button b;
Intent i;
static int cameraData =0;
Bitmap bmp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
cleaning();
InputStream is=getResources().openRawResource(R.drawable.ic_launcher);
bmp=BitmapFactory.decodeStream(is);
}
private void cleaning() {
imgb=(ImageButton) findViewById(R.id.imgbutt);
imgv=(ImageView) findViewById(R.id.iv);
b=(Button) findViewById(R.id.butt);
imgb.setOnClickListener(this);
b.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.imgbutt:
i=new Intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
case R.id.butt :
try {
getApplicationContext().setWallpaper(bmp);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
Bundle extras=data.getExtras();
bmp=(Bitmap) extras.get("data");
imgv.setImageBitmap(bmp);
}
}
}
The problem is every time I hit "take pic" button I get an error say:
the application has stopped unexpectedly
Some tips from when I have to troubleshoot is just simply using logcat.
This guy explains it well. http://www.youtube.com/watch?v=lESZqCflB0o&feature=bf_next&list=SPE953C0B85B50AB62&lf=list_related
Skip to 1:25:30
He will start right there about logs.
We'd all like to help but you really need to capture some details about what the error is in order for anyone to be able to try.
Please read up on how to use logcat and then use it to capture the actual error thats happening.