在我的应用中,我有一个图像视图。用户从库中选择图像并将其设置在图像视图上。但是每当用户从图库中选择相机图片时,相机图片总是会旋转90度。这是我的代码。
MainActivity
public class MainActivity extends AppCompatActivity {
public static final String LOG_TAG = "MainActivity";
ImageView imageView;
EditText number, name;
Button setTimeButton;
ImageButton imageButton;
TimePickerDialog timePicker;
long selectedTimeInMillis;
String enteredName, enteredNumber;
private static final int SELECT_IMAGE = 100;
public static final int REQUEST_CODE_PI = 1001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PreferenceManager.setDefaultValues(this, R.xml.settings, false);
imageView = findViewById(R.id.image_view);
setTimeButton = findViewById(R.id.set_time);
imageButton = findViewById(R.id.place_call_button);
number = findViewById(R.id.number);
name = findViewById(R.id.name);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, SELECT_IMAGE);
}
});
setTimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
timePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
selectedTimeInMillis = calendar.getTimeInMillis();
if (hourOfDay > 12) {
hourOfDay = hourOfDay - 12;
}
Toast.makeText(MainActivity.this, hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();
Log.v(LOG_TAG, "Selected time in millis:" + selectedTimeInMillis);
}
}, hour, minute, false);
timePicker.show();
}
});
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
enteredName = name.getText().toString().trim();
enteredNumber = number.getText().toString().trim();
if (enteredNumber.isEmpty()){
Toast.makeText(MainActivity.this, "Please enter a number", Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(MainActivity.this, AlarmReciever.class);
intent.putExtra("FAKE_NAME", enteredName);
intent.putExtra("FAKE_NUMBER", enteredNumber);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), REQUEST_CODE_PI, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
if (alarmManager != null) {
alarmManager.set(AlarmManager.RTC_WAKEUP, selectedTimeInMillis, pendingIntent);
Toast.makeText(MainActivity.this, "Your call has been placed", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Error: something wrong", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
case R.id.quick_call:
quickCall();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
Uri imageUri = data.getData();
InputStream imageStream = null;
if (imageUri != null) {
imageStream = getContentResolver().openInputStream(imageUri);
}
Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "You haven't picked any Image", Toast.LENGTH_SHORT).show();
}
}
public void quickCall(){
Calendar calendar = Calendar.getInstance();
calendar.get(Calendar.HOUR_OF_DAY);
calendar.get(Calendar.MINUTE);
long millis = calendar.getTimeInMillis();
Intent quickCallIntent = new Intent(MainActivity.this, AlarmReciever.class);
quickCallIntent.putExtra("QUICK_FAKE_NAME", "Shanks");
quickCallIntent.putExtra("QUICK_FAKE_NUMBER", "+91 98554 34734");
PendingIntent quickCallPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), REQUEST_CODE_PI, quickCallIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
if (alarmManager != null) {
alarmManager.set(AlarmManager.RTC_WAKEUP, millis + 10000, quickCallPendingIntent);
Toast.makeText(MainActivity.this, "Your call has been placed", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Error: something wrong", Toast.LENGTH_SHORT).show();
}
}
}
我该怎么做才能解决这个问题?
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mani.fakecall.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_gravity="center_horizontal"
android:shape="oval"
app:cardBackgroundColor="@android:color/darker_gray"
app:cardCornerRadius="48dp">
<ImageView
android:id="@+id/image_view"
android:contentDescription="@string/desc_select_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:srcCompat="@android:drawable/ic_menu_camera" />
</android.support.v7.widget.CardView>
<EditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="@string/display_number"
android:inputType="phone" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="@string/display_name"
android:inputType="textCapWords" />
<Button
android:id="@+id/set_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"
android:text="@string/set_time"
android:textSize="16sp" />
<ImageButton
android:id="@+id/place_call_button"
android:contentDescription="@string/desc_fake_call_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/circular_button"
android:backgroundTint="@color/color_place_call_button"
android:src="@drawable/ic_call_white_24dp"
android:layout_marginTop="16dp" />
</LinearLayout>
</ScrollView>