2016-03-06 77 views
0

我是編程的初學者。我正在嘗試創建Meme生成器應用程序。我已經到了在GridView中顯示圖像陣列的地步,用戶可以點擊一個圖像,然後將它們帶到一個新的活動,用戶可以輸入頂部和底部文本。通過使用URI的意圖傳遞圖像到另一個活動

基於我的代碼,我將不勝感激您的幫助。

  • A)我想在一個位圖轉換相對佈局(二TextViews和一個ImageView的)。代碼是否正確?
  • - B)當用戶點擊創建按鈕時,我想將從相對佈局創建的位圖發送到FullImage.java活動。您認爲 代碼是否正確?
  • - C)當用戶點擊創建按鈕時,目前我得到「失敗的聯編程序事務」。我認爲它是因爲我通過了一張 圖片。我試圖理解並使用如何傳遞URI,但不是
    成功。我試圖在SO和其他博客上找到片段,但是 不成功。你有可能給我詳細的代碼嗎? (作爲 初學者,生病是感謝!)

DetailsActivity.java

package com.jobaka.www.jobaka; 

import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

/** 
* Created by Psp on 2016-03-03. 
*/ 
public class DetailsActivity extends AppCompatActivity { 

    EditText etTop, etBottom; 
    TextView tvTop, tvBottom; 
    Button bCreate; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.details_activity); 

     Intent intent = getIntent(); 
     int id = intent.getIntExtra("id", -1); 
     if(id != -1){ 
      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), id); 
      ImageView imageView = (ImageView) findViewById(R.id.image); 
      imageView.setImageBitmap(bitmap); 
     }else{ 

     } 

     //imageUri = Uri.parse(extras.getString("imageUri")); 

     etTop = (EditText) findViewById(R.id.etTop); 
     tvTop = (TextView) findViewById(R.id.tvTop); 
     etTop.addTextChangedListener(new TextWatcher() { 
      public void afterTextChanged(Editable s) { 
       tvTop.setText(s.toString()); 
      } 

      public void beforeTextChanged(CharSequence s, int start,int count, int after) {} 

      public void onTextChanged(CharSequence s, int start,int before, int count) {} 
     }); 

     etBottom = (EditText) findViewById(R.id.etBottom); 
     tvBottom = (TextView) findViewById(R.id.tvBottom); 
     etBottom.addTextChangedListener(new TextWatcher() { 
      public void afterTextChanged(Editable sb) { 
       tvBottom.setText(sb.toString()); 
      } 

      public void beforeTextChanged(CharSequence sb, int start,int count, int after) {} 

      public void onTextChanged(CharSequence sb, int start,int before, int count) {} 
     }); 

     bCreate = (Button) findViewById(R.id.bCreate); 
     bCreate.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       RelativeLayout v = (RelativeLayout)findViewById(R.id.iFrame); 
       Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), 
         Bitmap.Config.ARGB_8888); 
       Canvas canvas = new Canvas(bitmap); 
       view.draw(canvas); 

       Intent sendImage = new Intent(DetailsActivity.this, FullImage.class); 
       sendImage.putExtra("pic", bitmap); 
       startActivity(sendImage); 
      } 
     }); 




    } 


} 

FullImage.java

package com.jobaka.www.jobaka; 

import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.ImageView; 

public class FullImage extends AppCompatActivity { 

    ImageView finalimage; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_full_image); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     Intent startingIntent = getIntent(); 
     String whatYouSent = startingIntent.getStringExtra("pic"); 
     Bitmap bitmap = BitmapFactory.decodeFile("pic"); 
     finalimage = (ImageView) findViewById(R.id.ivFull); 
     finalimage.setImageBitmap(bitmap); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
    } 

} 

回答

0

在你FullImageActivity,這樣做是爲了獲取位圖

Intent intent = getIntent(); 
    Bitmap bitmap = (Bitmap) intent.getParcelableExtra("pic"); 

這是因爲bi android中的tmap類實現了parcelable。 http://developer.android.com/reference/android/graphics/Bitmap.html

+0

我刪除了以下幾行。並添加你給的東西。當我點擊DeatailsActivity.java上的創建按鈕時,仍然沒有任何反應。它應該帶我到FullImage活動。另外,你可以通過uri傳遞圖像的任何示例嗎?這是我認爲的第二個問題。感謝您的及時答覆。 // String whatYouSent = startingIntent.getStringExtra(「pic」); //位圖位圖= BitmapFactory.decodeFile(「pic」); –

相關問題