Android Simple App加载图片不起作用

时间:2017-05-02 20:55:57

标签: java android android-intent

对于模糊的标题感到抱歉,我真的不知道我在我的应用程序中做错了什么。我是Android的新手,所以请耐心等待。

这是Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.phamt.ubung11">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:debuggable="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".MyReceiver">
             <intent-filter>
                    <action android:name="com.example.loadpicservice" />
             </intent-filter>
        </receiver>
        <service
            android:name=".LoadPicService"
            android:enabled="true"
            android:exported="true"></service>
    </application>

</manifest>

我的MainActivity班级:

public class MainActivity extends AppCompatActivity {
    private static String url1 = "https://lh3.googleusercontent.com/j5fD3m5qXRNtGYDuajhEtS_etFvU8FE5PogmqTY2hrshDG0_urf_UBeVAyJljoCxdf4=w300";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button buttonGo  = (Button) findViewById(R.id.button4);
        buttonGo.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick (View v) {
                        TextView textView = (TextView) findViewById(R.id.textView1);
                        textView.setText("Now Click Kill Button to delete pictures");
                        Intent intent = new Intent(MainActivity.this, LoadPicService.class);
                        intent.putExtra("url", MainActivity.url1);
                        startService(intent);
                    }

                }
        );
        Button buttonKill = (Button) findViewById(R.id.button);
        buttonKill.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick( View v) {
                        System.exit(0);
                    }
                }
        );
        MyReceiver receiver = new MyReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                byte[] byteArray = getIntent().getByteArrayExtra("bild");
                Bitmap bmp = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
                //funktion SetImage here !
                ImageView img1 = (ImageView) findViewById(R.id.imageView2);
                img1.setImageBitmap(bmp);
                img1.setVisibility(View.VISIBLE);
            }
        };
        IntentFilter intentFilter = new IntentFilter("com.example.loadpicservice");
        registerReceiver(receiver,intentFilter);
    }
}

最后,我的Service班。

public class LoadPicService extends IntentService {

    public LoadPicService() {
        super("LoadService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        String urls = intent.getStringExtra("url");
        Intent ansIntent = new Intent(this, LoadPicService.class);
        Bitmap bmp = null;
        try {
            URL url = new URL(urls);
            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        } catch (Exception e) {
            Log.i("cannot download picture", "CANT DOWNLOAD PICTURE");
        }
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        ansIntent.putExtra("bild",byteArray);
        ansIntent.setAction("com.example.loadpicservice");
        //ggbf.  runterskalieren.
        sendBroadcast(ansIntent);


    }



    public void onStartCommand(Intent intent, int startId) {

    }
}

简而言之,这就是我所做的事情。

  1. 我创建了服务,该服务在您点击按钮Go时启动,该按钮会发送Intent,其网址为Extra
  2. 启动服务时会加载图片并将其转换为ByteArray并通过新意图
  3. Extra发送至MainActivity
  4. 在MainActivity中,接收广播后,将给定的ByteArray转换为BitMap,并将当前的ImageView设置为该图片。
  5. 编辑:图片无法加载,我检查了网址是否正常。否则OnClick方法有效。 我在这做错了什么?谢谢你的帮助!

0 个答案:

没有答案