我很少询问stackoverflow,但我最近问了一个问题,得到了一个很好的答案!所以我再试一次!
我一直致力于开发一款非常简单的照片过滤器应用,可以通过代码添加效果。
我一直在努力的是每当我打开画廊时都会发送图片,以便为另一项活动选择一张图片。我的意思是,我想从图库中选择一个图像,然后将其发送到预览活动。
每当我从相机拍摄照片时,它都能正常工作,我会使用添加的滤镜拍摄照片,但问题是每当我从照片库中选择一张图片时,我的应用就崩溃了。我尝试使用URI,但它没有成型,所以我宁愿将我拍摄的图片转换为位图,然后将其发送到活动。有人可以帮我这个吗?
谢谢!
MainAcitivty
package com.example.alek.leszealots;
import java.io.File;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends AppCompatActivity {
//Deklarasjoner for fremtidlig var
Button galleryButton;
Button cameraButton;
public static final int PICK_IMAGE = 1;
private static final int CAMERA_REQUEST = 1;
Bitmap thumbnail;
int CAPTURE_REQUEST;
Uri thumbFile;
File mTempFile;
int REQUEST_CODE_CHOOSE_PICTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Gjør klar menu knappene
galleryButton = (Button) findViewById(R.id.button);
cameraButton = (Button) findViewById(R.id.button2);
//Gallery knapp
galleryButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
/*Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
startActivityForResult(chooserIntent, PICK_IMAGE);*/
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, false);
startActivityForResult(Intent.createChooser(photoPickerIntent,"Complete Action Using"), PICK_IMAGE);
}
});
//Camera knapp
cameraButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAPTURE_REQUEST);
}
});
}
//Sender lagret bilde i bitmap til sendImage som deretter sender videre til NextActivity i extra
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
sendImage();
}
else if (requestCode == PICK_IMAGE) {
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
package test.test;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class NextActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
//Ser ut som denne kan ta imot fra Gallery option også.
Bundle extras = getIntent().getExtras();
if (extras != null) {
Bitmap image = (Bitmap) extras.get("image");
if (image != null) {
imageView.setImageBitmap(changeBitmapContrastBrightness(image, 20, -255));
}
}
}
public static Bitmap changeBitmapContrastBrightness(Bitmap bmp, float contrast, float brightness)
{
ColorMatrix cm = new ColorMatrix(new float[]
{
contrast, 0, 0, 0, brightness,
0, contrast, 0, 0, brightness,
0, 0, contrast, 0, brightness,
0, 0, 0, 1, 0
});
Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(ret);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bmp, 0, 0, paint);
return ret;
}
}
activity_next
<ImageView
android:id="@+id/imageView"
android:layout_width="330dp"
android:layout_height="369dp"
app:srcCompat="@mipmap/ic_launcher"
tools:layout_editor_absoluteX="38dp"
tools:layout_editor_absoluteY="37dp" />
</android.support.constraint.ConstraintLayout>
activity_main
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="105dp"
tools:layout_editor_absoluteY="189dp" />
</android.support.constraint.ConstraintLayout>
编辑: