Issue
My Goal: To fix this error and be able to run my app without an error.
Error Message: Note: D:\Learning\app\src\main\java\com\example\learning\MainActivity.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.
I get the following error in 'Build Output'. I tried to fix this problem, but every try was unsuccessful. I found some questions related to this error on Stackoverflow, but it didn't meet my needs, so I decided to ask my own question in hope if someone can help me with this error.
public class MainActivity extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
String currentPhotoPath;
Button sendBtn;
EditText nameText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameText = (EditText)findViewById(R.id.editText);
sendBtn = (Button)findViewById(R.id.button2);
}
public void dispatchTakePictureIntent(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}catch (ActivityNotFoundException e){
//dispaly error
}
}
private File createImageFile() throws IOException{
//Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = " JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
public void onClick(View view){
dispatchTakePictureIntent();
}
public void onClick2(View view) {
SqlAdapter sqlAdapter = new SqlAdapter();
try {
Connection con = sqlAdapter.connectionclass();
String query = "INSERT INTO dbo.Test(text) VALUES ('" + nameText.getText().toString() + "') ";
Statement stmt = con.createStatement();
stmt.executeUpdate(query);
}
catch (SQLException sqlException){
Log.e("ERROR", sqlException.getMessage());
}
}
}
Thanks in advance!
Solution
startActivityForResult(…)
is deprecated in AndroidX activity (AppCompatActivity
).
They created a replacement named registerForActivityResult(…)
in ComponentActivity
(base-base class of AppCompatActivity
) which should be better.
See my answer in another question and documentation for registerForActivityResult(…)
.
Answered By - Shlomi Katriel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.