我正在努力创建一款平均颜色和内容的应用,我拥有它。我在ImageView
中获得了我的颜色并将其变成了位图,我想将其保存在SD卡上。但是当我按下按钮试图让它这样做时,它会崩溃。
我也希望能给它带来它的'保存时拥有自己的文件夹,以及自定义文件名。 (在一个理想的世界里,我喜欢每次增加一个,比如 Swatch_01.png , Swatch_02.png 等)
如果它简单,那么我可能会哭一点。
编辑:谢谢你们,它已经修好了。它没有创建JAverager_Swatches文件夹,而且imageView没有正确转换为位图。经过一段时间的睡眠后,我看了一下它并设法解决了它。
package com.colours.javerager;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class AverageImageActivity extends Activity {
public static final int MEDIA_TYPE_IMAGE = 1;
public TextView red;
public TextView green;
public TextView blue;
public TextView hex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_average_image);
ActionBar actionBar = getActionBar();
actionBar.hide(); //hides action bar
final int avgColour2 = getIntent().getExtras().getInt("totalvalue");
ImageView imgtest = (ImageView)findViewById(R.id.averageColourGenerated);
imgtest.setBackgroundColor(avgColour2);
final int r = getIntent().getExtras().getInt("redValue");
final int g = getIntent().getExtras().getInt("greenValue");
final int b = getIntent().getExtras().getInt("blueValue");
final String hx = getIntent().getExtras().getString("hexValue");
red = (TextView)findViewById(R.id.redTxt);
red.setText("R: " + r);
green = (TextView)findViewById(R.id.greenTxt);
green.setText("G: " + g);
blue = (TextView)findViewById(R.id.blueTxt);
blue.setText("B: " + b);
hex = (TextView)findViewById(R.id.hexTxt);
hex.setText("Hex: #" + hx);
}
public void clickSaveButton(View v){
final int avgColour2 = getIntent().getExtras().getInt("totalvalue");
ImageView imgtest = (ImageView)findViewById(R.id.averageColourGenerated);
imgtest.setBackgroundColor(avgColour2);
BitmapDrawable drawable = (BitmapDrawable) imgtest.getDrawable();
final Bitmap bmpimg = drawable.getBitmap();
//turns imageView into a bitmap to save it
OutputStream outStream = null;
File file = new File(Environment.getExternalStorageDirectory() + "/Javerager_Swatches/", "Swatch.PNG");
try {
outStream = new FileOutputStream(file);
bmpimg.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
public void clickHomeButton(View v){
Intent intent = new Intent(this, MenuScreenActivity.class);
startActivity(intent);
}
编辑:有很多logcat的东西,它很红,很生气;〜;
04-19 23:30:54.308:E / AndroidRuntime(1184):致命异常:主要
04-19 23:30:54.308:E / AndroidRuntime(1184):流程: com.colours.javerager,PID:1184
04-19 23:30:54.308: E / AndroidRuntime(1184):java.lang.IllegalStateException:无法 执行活动方法
04-19 23:30:54.308: E / AndroidRuntime(1184):at android.view.View $ 1.onClick(View.java:3814)
04-19 23:30:54.308: E / AndroidRuntime(1184):at android.view.View.performClick(View.java:4424)
04-19 23:30:54.308: E / AndroidRuntime(1184):at android.view.View $ PerformClick.run(View.java:18383)
04-19 23:30:54.308:E / AndroidRuntime(1184):at android.os.Handler.handleCallback(Handler.java:733)
04-19 23:30:54.308:E / AndroidRuntime(1184):at android.os.Handler.dispatchMessage(Handler.java:95)
04-19 23:30:54.308:E / AndroidRuntime(1184):at android.os.Looper.loop(Looper.java:137)
04-19 23:30:54.308: E / AndroidRuntime(1184):at android.app.ActivityThread.main(ActivityThread.java:4998)
04-19 23:30:54.308:E / AndroidRuntime(1184):at java.lang.reflect.Method.invokeNative(Native Method)
04-19 23:30:54.308:E / AndroidRuntime(1184):at java.lang.reflect.Method.invoke(Method.java:515)
04-19 23:30:54.308:E / AndroidRuntime(1184):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:777)
04-19 23:30:54.308:E / AndroidRuntime(1184):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-19 23:30:54.308:E / AndroidRuntime(1184):at dalvik.system.NativeStart.main(原生方法)
04-19 23:30:54.308: E / AndroidRuntime(1184):引起: java.lang.reflect.InvocationTargetException
04-19 23:30:54.308: E / AndroidRuntime(1184):at java.lang.reflect.Method.invokeNative(Native Method)
04-19 23:30:54.308:E / AndroidRuntime(1184):at java.lang.reflect.Method.invoke(Method.java:515)
04-19 23:30:54.308:E / AndroidRuntime(1184):at android.view.View $ 1.onClick(View.java:3809)
04-19 23:30:54.308: E / AndroidRuntime(1184):... 11更多
04-19 23:30:54.308: E / AndroidRuntime(1184):引起:java.lang.NullPointerException
04-19 23:30:54.308:E / AndroidRuntime(1184):at com.colours.javerager.AverageImageActivity.clickSaveButton(AverageImageActivity.java:81)
04-19 23:30:54.308:E / AndroidRuntime(1184):... 14更多
答案 0 :(得分:0)
请检查两个主题。比如
AndroidManifest.xml中的#1#检查权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
number 2#检查Javerager_Swatches是否存在SD卡中的目录。