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.
Related
I feel sorry to come and ask people for help, but I've been stuck with this problem for a while and it's driving me mad.
I'm trying to take a button from MainActivity where it puts you into the camera and takes a picture. It then takes you to the next Activity where there is a preview of the very same image. But whenever I arrive to the next activity the image doesn't show up, just a placeholder. What am I doing wrong? Am I missing something in the code?
I've been looking around and tried to use Uri, but apparently it's not permitted anymore as you need to give specific permissions for the action, so FileProvider is recommended, but honestly It's a little bit too hard for a beginner like me. I tried and failed for hours.
MainActivity
public class MainActivity extends AppCompatActivity {
int CAPTURE_REQUEST;
Button button;
Bitmap thumbnail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAPTURE_REQUEST);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
sendImage();
}
}
}
private void sendImage() {
Intent intent = new Intent(MainActivity.this, NextActivity.class);
intent.putExtra("image", thumbnail);
startActivity(intent);
}}
NextActivity
public class NextActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
ImageView imageView = (ImageView) findViewById(R.id.imageView);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
Bundle extras = getIntent().getExtras();
if (extras != null) {
Bitmap image = (Bitmap) extras.get("image");
if (image != null) {
imageView.setImageBitmap(image);
}
}
}}
Change onActivityResult to
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
sendImage();
}
}
You are overriding your global variable with the local declaration.
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.
The Media Player work well when I start the app and click the button. However, when I hit the Home button or when I go to another activity and then I cannot play Media file any more when I come back to the MainActivity. Here is my code:
public void playMedia(View view){
if(mp.isPlaying()){
mp.pause();
}
else
mp.start();
}
#Override
protected void onPause(){
super.onPause();
SOSPlayer.pause();
}
#Override
protected void onResume(){
super.onResume();
SOSPlayer.seekTo(0);
}
#Override
protected void onDestroy() {
super.onDestroy();
SOSPlayer.stop();
SOSPlayer.release();
}
#Override
protected void onStop() {
super.onStop();
SOSPlayer.stop();
}
I thing you call playMedia method on layout as:
<Button
...
android:onClick="playMedia"
..
/>
What you need is to play that media again after getting back from previous activity .. you need to start that activity for result with startActivityForResult and listen to feedback onActivityResult to play the media again. Here is a simple implementation:
private static final int REQUEST_ID = 0x0001;
private void startActivity() {
Intent intent = new Intent(getApplicationContext(), DifferentActivity.class);
startActivityForResult(intent, REQUEST_ID);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ID:
playMedia(null);
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
Guys I have 2 activity when I clicked button in main starting new activity(message.java) and I'm trying to get data from my message activity
to mainActivity .In message activity, I have edittext and when clicked button go back mainactivity and show that info in textview. This code working but problem is when I clicked button in mainActivity
it's first delete textview's text and then message activity begining . This is not what I want and also , it's taking info from message activity that's OK but in mainActivity I must clicked again mainActivtiy button to show in textview. Any suggest?
This is what I want
This picture shows problem
Parent -> MainActivity
Child ->message
public class MainActivity extends AppCompatActivity {
private RelativeLayout rLayout;
private Button bt,bt2;
private TextView tv;
private String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
tv=(TextView)findViewById(R.id.textView2);
bt2=(Button)findViewById(R.id.button4);
final Intent i=new Intent(getBaseContext(),MessageActivity.class);
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(i, 1);
tv.setText(s);
}
});
}//end of onCreate
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
s=data.getStringExtra("info");
}
}
}
}//end of mainActivity
here message.java
public class MessageActivity extends Activity {
private Button bt;
private TextView tv;
private EditText et;
private String s;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.message);
bt=(Button)findViewById(R.id.button);
et=(EditText)findViewById(R.id.editText);
tv=(TextView)findViewById(R.id.textView);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
s=et.getText().toString();
data.putExtra("info", s);
setResult(RESULT_OK, data);
finish();
}
});
}
}
Im not sure if I understood your question correctly, but if you would like to show your text in MainActivity when you come back from MessageActivity, you should just be setting the text in onActivityResult() method, not in the clickListener.
So your code will look like this:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
s=data.getStringExtra("info");
tv.setText(s);
}
}
}
Why don't you try to set the textView text in the activity result function. Is the better place to do it. Think than when you start a child Activity, the flow of parent activity stops. And when you finish child activity, flow goes directly to onActivityResult function. Try it. Regards.
In your MainActivity you need to set the text on your textview once you receive it. Problem is that startActivityForResult is asynchronous call. Once call to this method is gone it will start other activity but your onClick() method will not stop here. Its a normal listener that calls onActivityResult when your activity is destroyed.
public class MainActivity extends AppCompatActivity {
private RelativeLayout rLayout;
private Button bt,bt2;
private TextView tv;
private String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
tv=(TextView)findViewById(R.id.textView2);
bt2=(Button)findViewById(R.id.button4);
final Intent i=new Intent(getBaseContext(),MessageActivity.class);
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(i, 1);
}
});
}//end of onCreate
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
s=data.getStringExtra("info");
tv.setText(s);
}
}
}
}
Accept and upvote if it works.
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);
}
}
}